If the structure is like a.b(1,1).c then how to get the value of 'c' using matlab mex
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
If the structure is a.b.c then I kow below will be the code:
For a value
const mxArray *L1=mxGetField(*Pointer array, index,"field_name");
For b value
const mxArray *L2= mxGetField(*L1,index,"field_name");
For C value
const mxArray *L3 = mxGetField(*L2, index, "field_name");
But how to acccess data for a.b(1,1).c?
1 Kommentar
James Tursa
am 13 Mai 2024
Bearbeitet: James Tursa
am 13 Mai 2024
Please post a small example mat file with a variable that has exactly the layout you are working with, or perhaps some m-code that will build such a small example variable, and then we can post the exact C code to extract what you want.
Antworten (1)
Harsh
am 8 Mai 2024
Bearbeitet: Harsh
am 8 Mai 2024
Hi,
From what I can gather, you are working with a mex file and are trying to use the "mxGetField" function to fetch "a.b(1,1).c".
Please follow the following steps to retrieve the value of "a.b(1, 1).c" using the function defined from the mex file.
(sample.c)
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
// Check the number of input and output arguments.
if(nrhs != 1)
mexErrMsgIdAndTxt("MATLAB:mexFunction:invalidNumInputs",
"One input required.");
if(nlhs > 1)
mexErrMsgIdAndTxt("MATLAB:mexFunction:maxlhs",
"Too many output arguments.");
// Assume the input is the structure 'a'.
const mxArray *a = prhs[0];
// Access 'b' field of 'a'.
mxArray *b = mxGetField(a, 0, "b");
if (b == NULL)
mexErrMsgIdAndTxt("MATLAB:mexFunction:fieldNotFound",
"Field 'b' not found.");
// Access 'c' field of 'b'.
mxArray *c = mxGetField(b, 0, "c");
if (c == NULL)
mexErrMsgIdAndTxt("MATLAB:mexFunction:fieldNotFound",
"Field 'c' not found.");
// Assuming 'c' contains a scalar double for simplicity.
if (!mxIsDouble(c) || mxGetNumberOfElements(c) != 1)
mexErrMsgIdAndTxt("MATLAB:mexFunction:invalidField",
"Field 'c' is not a scalar double.");
// Get the value of 'c'.
double value = *mxGetPr(c);
// Return the value of 'c' as output.
plhs[0] = mxCreateDoubleScalar(value);
}
Now, copy the following commands in MATLAB console.
mex sample.c
a.b(1, 1).c = 233;
res = sample(a)
I hope this helps, thanks!
2 Kommentare
Harsh
am 9 Mai 2024
Bearbeitet: Harsh
am 9 Mai 2024
Hi Rajesh,
The following code can be used to extract "a.b{1, 2}.c{1, 1}{1, 2}", you can make relevant changes to the following code to extract data from the structure you are working with.
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
// Check the number of input and output arguments.
if(nrhs != 1)
mexErrMsgIdAndTxt("MATLAB:mexFunction:invalidNumInputs",
"One input required.");
if(nlhs > 1)
mexErrMsgIdAndTxt("MATLAB:mexFunction:maxlhs",
"Too many output arguments.");
// Assume the input is the structure 'a'.
const mxArray *a = prhs[0];
mxArray *b = mxGetField(a, 0, "b"); // for a.b
mwSize numRowsA = mxGetM(a);
// Here, we are trying to excess {1, 1}
mwSize rowIdxA = 1;
mwSize colIdxA = 1;
mwIndex bIndex = mxCalcSingleSubscript(b, 0, (mwIndex[]){rowIdxA + colIdxA - 2}); // for a.b{1,1}
mxArray *bB = mxGetCell(b, bIndex);
// get b{1, 2}.c
mwSize rowIdxbB = 1;
mwSize colIdxbB = 2;
mwIndex bBIndex = mxCalcSingleSubscript(bB, 0, (mwIndex[]){rowIdxbB + colIdxbB - 2}); // Convert to linear index
mxArray *c = mxGetCell(bB, bBIndex); // for a.b{1,1}.c
// Get c{1,1}
mwSize rowIdxC = 1;
mwSize colIdxC = 1;
mwIndex cIndex = mxCalcSingleSubscript(c, 0, (mwIndex[]){rowIdxC + colIdxC - 2}); // Convert to linear index
mxArray *cC = mxGetCell(c, cIndex); // for a.b{1,1}.c{1,1}
// Get c{1,1}{1,2}
mwSize rowIdxVal = 1;
mwSize colIdxVal = 2;
mwIndex cCIndex = mxCalcSingleSubscript(cC, 1, (mwIndex[]){rowIdxVal + colIdxVal - 2}); // Convert to linear index
mxArray *val = mxGetCell(cC, cCIndex); // for a.b{1,1}.c{1,1}{1,1}
if (val != NULL && mxIsDouble(val)) {
double value = *mxGetPr(val);
plhs[0] = mxCreateDoubleScalar(value);
}
else
mexErrMsgIdAndTxt("MyToolbox:arrayProduct:notDouble", "Cell does not contain a double.");
}
Now, let's create an example struct in MATLAB
c = {115, 111, 112};
b.c = {c, c, c};
a.b = {b, b, b}
Now, copy the following commands in MATLAB console.
mex sample.c
sample(a)
I hope this helps, thanks!
Siehe auch
Kategorien
Mehr zu Write C Functions Callable from MATLAB (MEX Files) finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!