Matlab mex function problem
Show older comments
hello, I am trying to use mex function to improve the performance of Matlab. In order to do that, I first tested this sample code (mexfile.c). But I am constantly getting an error which is immediately crashing Matlab. The problem is related to the function, mxCreateCellArray. Here is how I called my mexfile function from Matlab: [x,y,z]=mexfile(rand(3)). I have posted the code below. Please someone help me to solve the problem.
// mexfile.c
#include <stdio.h>
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
double *input, *output, *output2;
int i, j, n_rows, n_cols;
mwSize size[2];
size[0] = 2;
size[1] = 2;
input = mxGetPr(prhs[0]);
n_rows = mxGetM(prhs[0]);
n_cols = mxGetN(prhs[0]);
if (nlhs == 2) {
plhs[0] = mxCreateDoubleMatrix(n_rows, 1, mxREAL);
output = mxGetPr(plhs[0]);
plhs[1] = mxCreateDoubleMatrix(n_rows, 1, mxREAL);
output2 = mxGetPr(plhs[1]);
plhs[2] = mxCreateCellArray(2, &size);
}
for(i=0; i<n_rows; i++) {
avg = 0.;
for(j=0; j<n_cols; j++) {
avg += input[(i*n_cols)+j];
output2[i] = i;
}
avg /= (double)n_cols;
output[i] = avg;
}
} /* end mexFunction */
Accepted Answer
More Answers (0)
Categories
Find more on Write C Functions Callable from MATLAB (MEX Files) in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!