MEX: iterator and mxArray
Mostrar comentarios más antiguos
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 comentarios
James Tursa
el 7 de Jul. de 2011
What do you mean, exactly, by "extract"? Do you mean you need to index into the array to read these values in a loop? Do you mean you need to build a new array that contains these values? Or something else?
Lea L
el 8 de Jul. de 2011
Jan
el 8 de Jul. de 2011
I still do not get it. Does "prhs[1] = {2, [5:12]}" mean that you want to get the subarray "A(2, 5:12)" as output, if A is the input?
Use mxDuplicateArray or memcpy to duplicate an complete array.
Lea L
el 8 de Jul. de 2011
Lea L
el 8 de Jul. de 2011
Lea L
el 11 de Jul. de 2011
Respuestas (1)
Jan
el 11 de Jul. de 2011
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.
Categorías
Más información sobre Write C Functions Callable from MATLAB (MEX Files) en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!