Need to find out errors in mex

1 view (last 30 days)
Abeera Tariq
Abeera Tariq on 30 May 2015
Edited: Abeera Tariq on 2 Jun 2015
I am trying to make file of this code. mex file is created but when i try to use this mex file my matlab crashes can someone locate the error?
float **sum;
sum= (float *)mxMalloc(sizeof(float)*q*f2);
I am feeling like i am hitting my head against the wall.. Pleaseeee help
Abeera Tariq
Abeera Tariq on 31 May 2015
I am using
>> X=ones(20,4);
>> X=single(X);
>> I=ones(9,8);
>> prp(X,1,1,1,1,1,I);
Prp is the mex file created by this code

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 31 May 2015
Edited: James Tursa on 1 Jun 2015
Remember the advice I gave you about abandoning your desire to maintain the multi-level [ ][ ] indexing scheme when using dynamic memory? Your posted code is a perfect example of why I gave that advice. I will focus on only one of the variables to show the problem:
double **idx1;
idx1= (double *)mxMalloc(sizeof(double)*rmax*cmax);
for (i = 0; i < rmax; i++){
for( j = 0 ; j <cmax ; j++ ){
idx1[i][j]= I[i][j]; }}
idx1 is of type "pointer to pointer to double". That is, it points to memory that is supposed to contain "pointer to double"s. Your allocation is all wrong. It should be something like this:
idx1= (double **)mxMalloc(sizeof(double *) * SOMETHING);
Then you can fill idx1 with other pointers that point to the actual double data. What you CAN'T do is use the [ ][ ] indexing with it until you have first filled in the pointer values. I.e., this line will certainly crash MATLAB since idx1[i] is an uninitialized value (contains an invalid pointer):
idx1[i][j]= I[i][j];
I don't have time at the moment to write up a proper way to do this in C, but will try to do so this evening.
EDIT: 6/1/2015
OK, here is a short example showing how to allocate a 2D matrix dynamically (i.e. you don't know the sizes ahead of time so the sizes are not constant) and still use the [ ][ ] indexing syntax. The keys are that each level of [ ] requires a separate allocation, and that you need to fill in the row pointers manually in order to get the [ ][ ] syntax to work.
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
double **x;
double *d;
mwSize i, j, m, n;
m = 3;
n = 4;
d = (double *) mxMalloc( m * n * sizeof(*d) ); // Allocate the full matrix
x = (double **) mxMalloc( m * sizeof(*x) ); // Allocate the row pointers
for( i=0; i<m; i++ ) {
x[i] = d + i*n; // Set the row pointers to point inside the full matrix
for( i=0; i<m; i++ ) {
for( j=0; j<n; j++ ) {
x[i][j] = 10*(i+1) + (j+1); // Set the double data
mexPrintf("x[%d][%d] = %f\n",i,j,x[i][j]); // print it out
mxFree(x); // Free the row pointers
mxFree(d); // Free the double data
So you can now see the extra effort involved in getting this to work properly, and the only benefit is so that you can use the [ ][ ] syntax. And if you have a 3D array and want to use [ ][ ][ ] indexing, then you will need another level of allocation and another loop to manually fill in the pointers for this additional array.
  1 Comment
Abeera Tariq
Abeera Tariq on 2 Jun 2015
Thanks a lot James. You are just Awesome..

Sign in to comment.

More Answers (0)


Find more on MATLAB Compiler in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!