我正在尝试在mxArray
MATLAB
文件中动态声明mex
数组。
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
#define PRHS_NLEVELS prhs[0]
double *NLevel = mxGetPr( PRHS_NLEVELS );
int nLevel = (int) NLevel [0];
mxArray *Ain = (mxArray *) mxMalloc( nLevel * sizeof(mxArray) );
}
图像将输入到此mex
函数,Ain
会将其金字塔图像存储在不同的级别。我得到的编译错误如下:
mymex.cpp(59) : error C2027: use of undefined type 'mxArray_tag'
c:\program files\matlab\r2012b\extern\include\matrix.h(299) : see declaration of 'mxArray_tag'
答案 0 :(得分:1)
您可以创建矩阵的单元格数组,并从MEX函数返回。
示例:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
plhs[0] = mxCreateCellMatrix(1, 5);
for (mwIndex c=0; c<5; c++) {
mxArray *arr = mxCreateDoubleMatrix(3, 3, mxREAL);
double *x = mxGetPr(arr);
for (mwIndex i=0; i<9; i++) {
x[i] = c;
}
mxSetCell(plhs[0], c, arr);
}
}
>> c = test_cell_array()
c =
[3x3 double] [3x3 double] [3x3 double] [3x3 double] [3x3 double]
>> c{3}
ans =
2 2 2
2 2 2
2 2 2
在您的情况下,每个单元格将包含模糊的图像并在不同级别调整大小以创建图像金字塔,并将级别数指定为函数输入。
答案 1 :(得分:1)
我认为您只需要mxArray*
个数组,new
或malloc
很简单。你不能拥有mxArray
个数组,只能指向它们。
mxArray
来自typedef struct mxArray_tag mxArray;
,其中mxArray_tag
未定义,隐藏在MathWorks实现中。因此,你甚至不能像mxArray x[3];
那样简单,因为它是一种不完整的类型。你只能处理指向对象的指针。
<强> testMxArrayMEX.cpp 强>
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
int numLevels = 3;
//mxArray **matLevels = (mxArray **) malloc(numLevels * sizeof(mxArray**));
mxArray **matLevels = new mxArray*[numLevels];
matLevels[0] = mxCreateDoubleMatrix(2, 2, mxREAL);
matLevels[1] = mxCreateString("second");
matLevels[2] = mxCreateCellMatrix(1, 2);
mxSetCell(matLevels[2], 0, mxCreateString("third"));
mexPrintf("First:\n");
mexCallMATLAB(0, NULL, 1, matLevels, "disp");
mexPrintf("\nSecond:\n");
mexCallMATLAB(0, NULL, 1, &matLevels[1], "disp");
mexPrintf("\nThird:\n");
mexCallMATLAB(0, NULL, 1, matLevels+2, "disp");
// free(matLevels); // with malloc
delete[] matLevels; // with new
}
<强>输出强>
>> testMxArrayMEX
First:
0 0
0 0
Second:
second
Third:
'third' []