If the structure is like a.b(1,1).c then how to get the value of 'c' using matlab mex
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
James Tursa
2024-5-13
编辑:James Tursa
2024-5-13
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.
回答(1 个)
Harsh
2024-5-8
编辑:Harsh
2024-5-8
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 个评论
Harsh
2024-5-9
编辑:Harsh
2024-5-9
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!
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!