How do I pass a cell array from matlab to c code?

Hi every one,
my code snippet in matlab is,
L = zeros(numBlocksN, numBlocksO);
for r = 1:numBlocksN
for c = 1:numBlocksO
[rcEmd] = blockDistance(cell1{r}, cell2{c});
L(r,c) = rcEmd;
end
end
where numBlocksN & numBlocksO are constants, and cell1 & cell2 are cell arrays and the element of each (i.e., cell1{r} & cell2{c}) are structure arrays. The blockDistance inside loop is a self-defined fuction which computes the distance between cell1{r} and cell2{c}.
As is known, it is time-consuming to use loops in Matlab. So I want to translate this particular snippet into c source. I need to pass the integers numBlocksN, numBlocksO and two cell arrays ( cell1 and cell2 ) into c as arguments. And also, I want return the computed result L which carries the distance between each cell1{r} and cell2{c}. Could anyone kindly point out me a way to achieve this? Many thanks in advance.

 Accepted Answer

Cell arrays are passed exactly as any other type. I assume, your question concerns the usage of a cell inside a C-mex.
mxArray *c1, *c2;
double *OutputPtr;
plhs[0] = mxCreateDoubleMatrix(numBlocksN, numBlockO, mxREAL);
OutputPtr = mxGetPr(plhs[0]);
for (r = 0; r < numBlocksN; r++) {
c1 = mxGetCell(prhs[2], r);
for (c = 0; c < numBlocksO; c++) {
c2 = mxGetCell(prhs[3], c);
*OutputPtr++ = blockDistance(c1, c2); % EDITED: * added
end
end
Now lockDistance(c1, c2) gets pointers to the cell elements. Depending on your function lockDistance, you might need further e.g. mxGetPr calls.
BTW, your M-version can be made a littlte bit faster also:
L = zeros(numBlocksN, numBlocksO);
for c = 1:numBlocks0
c2 = cell2{c};
for r = 1:numBlocksN
L(r,c) = blockDistance(cell1{r}, c2);
end
end
In addition I would inline blockDistance and I guess it can be vectorized.

5 Comments

Hi Jan thanks a lot! Your answer makes great sense to my work. My only remaining query is how to return a 2-d array like what I have done in Matlab code instead of inlining it?
Forgot to mention, my method 'blockDistance' is a Matlab function, could I simply call it in c source like this?
The code I've posted does reply a 2D array already: plhs[0] = mxCreateDoubleMatrix(M, N, mxREAL) creates a MxN DOUBLE matrix, P=mxGetPr gets the pointer to the first element and *P++ write the value to a specific element and advances the pointer.
You can call a Matlab function using mexCallMATLAB, but this will be much slower than the pure Matlab implementation! It is not the FOR loop itself, which is slow in Matlab. Matlab is optimized for the handling of "Mat"rices, otherwise it would be called ElemLab or ScalarLab. Accessing a single element in a loop takes nearly the same time as accessing a complete matrix (a strong simplification, but at least a rule of thumb).
There I recommend: Either vectorize the blockDistance calculation in a pure Matlab implementation, or implement this function in C also.
Therefore I suggest
If blockDistance remains Matlab code you won't gain any significant in term of speed, as I explained in the News Group.
-To Jan Simon,
Actually my function 'blockDistance' is a matlab one (yet very simple, should not be time-consuming). I did the following things to call it in my c source,
mxArray *arguments[2];
arguments[0] = c1;
arguments[1] = c2;
mexCallMATLAB(1, lData++, 2, arguments, "blockDistance");
However the compiler returns me error. What's wrong with my code?

Sign in to comment.

More Answers (1)

The way how to pass an integer from matlab to c has been well studied. My major query is about the cell array.

Categories

Find more on MATLAB Compiler in Help Center and File Exchange

Tags

Asked:

on 27 Feb 2011

Community Treasure Hunt

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

Start Hunting!