MEX: iterator and mxArray

7 次查看(过去 30 天)
Lea L
Lea L 2011-7-7
Hey!
I've got a problem using iterators with mxArrays ! for now, i'm trying with something more linear but it doesn't work so good...
I use this function to read the whole buffer:
mxArray * bin = mxCreateNumericArray(ind_dim_order,dimsData,mxDOUBLE_CLASS, mxREAL);
double * outputbin = mxGetPr(bin);
for(size_t k =0;k<nbrSamplesTotal;k++,++tmp)
{
outputbin[k]=*(tmp);
}
plhs[2] = bin;
For example i get this (10*3 matrix) but i can also have more dimensions.
1 - 101 - 201
2 - 102 - 202
3 - 103 - 203
4 - 104 - 204
5 - 105 - 205
6 - 106 - 206
7 - 107 - 207
8 - 108 - 208
9 - 109 - 209
10 - 110 - 210
My problem is : I have a cellArray in prhs[1] which says what i have to extract in this buffer. For example i've got : prhs[1] = {2, [5:12]}. so I've got to extract only the line two for the first dimension and lines 5 until 12 for the second dimension.
Second example : prhs[1] = {{1,6},[ ],{5:10}} I've got to extract lines 1 and 6 for the first dimension, nothing for the second, and 5 until 10 for the third dimension.
I did : mxArray * dim1 = mxGetCell(prhs[1],ee) to extract dim1, dim2,dim3 and dim4 which correspond to each cell of the cellArray.
I'm thinking about iterators to extract the good data, but i don't know how to do this.
Sorry for my English, it's hard to explain about mathematics and logic...
Thanks a lot !
  6 个评论
Lea L
Lea L 2011-7-8
oops : A(5:12,2)
Lea L
Lea L 2011-7-11
do you have any idea, how to do that?

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2011-7-11
Extracting "A(index1, index2, ...)" in a MEX function is not trivial. But this is implemented in Matlab already. You can use mexCallMATLAB to call the SUBSREF command, but this needs a struct as input and it is not trivial to create it. I assume this is the easiest method:
function Y = Cut(X, varargin)
Y = X(varargin{:});
This can be called through mexCallMATLAB, if you really need it in a MEX. Implementing this in C will need more than 100 lines of code, because a lot of exceptions have to be considered: less indices than dimensions, more indices with trailing ones, ':' to specify the complete slice, indices as SINGLE or (U)INT8/16/32/64, logical indexing, rejection of complex and negative and non-integer values, linear indexing, ... Although a good C-implementation might be faster than calling the one-liner shown above through mexCallMATLAB, I bet that the programming and debug time will exceed the saved time by factor 1000.
  2 个评论
Lea L
Lea L 2011-7-12
thanks for your answer! I will use mxCallMATLAB then, but I don't really understand how to do that (I'm in intership and I've just begun in Mex and MATLAB...)
I don't understand :
function Y = Cut(X, varargin)
Y = X(varargin{:});
what is X ? do I have to create a function in MATLAB?
Lea L
Lea L 2011-7-12
I tried :
mxArray * bini = mxCreateNumericArray(ind_dim_order,dimsData,mxDOUBLE_CLASS, mxREAL);
double * outputbin = mxGetPr(bini);
for(size_t k =0;k<nbrSamplesTotal;k++,++tmp){outputbin[k]=*(tmp);}
mxArray *rhs[8];
mxArray * dimStruct;
rhs[0] = mxCreateString("."); rhs[1] = dim4;
rhs[2] = mxCreateString("."); rhs[3] = dim3;
rhs[4] = mxCreateString("."); rhs[5] = dim2;
rhs[6] = mxCreateString("."); rhs[7] = dim1;
cout<<"ok1"<<endl;
mexCallMATLAB(1, &dimStruct, 8, rhs, "substruct");
for(int t=0; t<8; t++) {mxDestroyArray(rhs[t]);}
plhs[2]= dimStruct;
cout<<"ok2"<<endl;
rhs[0] = (mxArray *)bini;
rhs[1] = dimStruct;
cout<<"ok3"<<endl;
mexCallMATLAB(1,&bin,2, rhs, "subsref");
cout<<"ok4"<<endl;
it's ok for compilation,the struct is created, but MATLAB is not working when i call subsref :
??? Error using ==> subsref
The "subs" field for the subscript argument to SUBSREF and SUBSASGN must be a cell or character array.
for remember : mxArray * dim1 = mxGetCell(prhs[1],ee);
and prhs[1] = {2, {5:9}};
and mxArray * bin;

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Write C Functions Callable from MATLAB (MEX Files) 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by