Accessing a cell array of cell arrays in a C++ mex file
4 ビュー (過去 30 日間)
古いコメントを表示
I'm writing a large code that requires me to use a cell array of cell arrays of single precision real matrices. This may seem odd, but there are good reasons centred on efficient storage, that I won't go into here. The simplest code that illustrates the problem that I have is:
#include "mex.h"
float fn(mxArray *strat)
{
float *indstrat, x;
indstrat = (float *)mxGetCell(mxGetCell(strat,0),0);
x = *(indstrat+1);
return x;
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[])
{
(void) nlhs; (void) plhs;
fn(prhs[0]);
}
There's one input, a cell array of cell arrays of the form, for example, strat{i}{j}(k). In this fragment, I've used mxGetCell twice to extract the pointer to the {1}{1} vector, and then extracted the second value from that using '*(indstrat+1)'. However, debugging this in Visual Studio shows that the values aren't being extracted properly. I must be doing something wrong with the pointer to the array of pointers implicit in a cell array like this, but I can't see what. Should there be a '**' somewhere for a pointer to a pointer?
Can anyone tell me what I'm doing wrong?
0 件のコメント
採用された回答
Geoff Hayes
2014 年 10 月 6 日
John - check the function signature for mxgetcell - it returns a pointer to an mxArray object. So you would have to do something like the following
float fn(const mxArray *strat)
{
mxArray *tempMxArray = 0;
float *indstrat, x;
tempMxArray = mxGetCell(mxGetCell(strat,0),0);
indstrat = (float *)mxGetPr(tempMxArray);
x = *(indstrat+1);
return x;
}
It is very similar to what you had, we just assign the result of the double mxGetCell call to the tempMxArray pointer, and then use mxGetPr to get a pointer to the first element in your single data type matrix.
I tried the above with the following example where the mexFunction is defined as
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
float x;
x=fn(prhs[0]);
mexPrintf("%f\n", x);
}
And in MATLAB, the following is executed from the Command Line
cellArray = {};
cellArray{1} = {single(1:4)};
cellTest(cellArray); % where cellTest is the name of the c-file with above code
2.000000 % 2 is the output of the mex function
Given that the input matrix is [1 2 3 4], the output of 2 makes sense. (I needed to add the const qualifier on the fourth input to the mexFunction.)
その他の回答 (0 件)
参考
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!