Hello everyone! I am starting to use C Mex files. So far, I'm running a test on how to work with multidimensional arrays. From Matlab, I'm sending an image (3 channels, so it will be mxnx3). The MEX File receives the images and calls another function (test). The only thing that my test function does, is to receive the image (mxnx3) and takes the first dimension (mxnx1) and multiply it by a scalar (a).
The problem is that my function only works when I send small arrays (4x3x3), but when I try to send an image, it crashes at the end (looks like it crashes when Max File finishes and return to Matlab).
My code is:
#include "mex.h"#include "matrix.h"void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ double *outMatrix; mwSize f,c,dims; double a; int *inMatrix; double *pr,*pi; inMatrix = (int *) mxGetData(prhs[0]); a = mxGetScalar(prhs[1]); const mwSize *dima = mxGetDimensions(prhs[0]); f=dima[0]; //Number of rows c=dima[1]; //Number of cols mwSize numel = mxGetNumberOfElements(prhs[0]); mexPrintf("a= %f\n",a); mexPrintf("rows= %d\n",f); mexPrintf("cols= %d\n",c); plhs[0] = mxCreateDoubleMatrix(f,c,mxREAL); outMatrix = mxGetPr(plhs[0]); test(inMatrix,outMatrix,f,c,a);} void test(double *inMatrix, double *outMatrix, int f, int c, double a){ double image[f][c],aux; int i,j,k; for (j=0; j<c; j++) for (i=0; i<f; i++) image[i][j]=inMatrix[f*j+i]*a; //Converting back the image to array for (j=0; j<c; j++) for (i=0; i<f; i++) outMatrix[f*j+i]=image[i][j]; }
From Matlab, I call the function like this:
B=[1 2 5 3; 7 8 9 6;1 4 5 6]; A(:,:,1)=B; A(:,:,2)=B; A(:,:,3)=B; C=mextest1(A,5);
And it works, because the returned matrix is:
C = 5 10 25 15 35 40 45 30 5 20 25 30
I need your help to understand what can be my error in the code. Thank you very much!
Best Answer