When I try to declare a matrix in C as having dimensions specified by a variable, I get an error message from the C compiler:
integer expression must be constant
where the integer referred to is a matrix dimension.
The problem is that I do not know the matrix dimension until run-time.
#include "mex.h"#include <stdio.h>/******* ROUTINE *******/void func(int z) { double A[z][5]; /* HERE'S THE PROBLEM: WANT TO MAKE MATRIX z-by-z, not 5-by-5 */ printf("z=%d\n", z); } /******* GATEWAY FUNCTION HERE TO LINK C & MATLAB **************/void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { int z; /* get scalar data from Matlab input */ z = mxGetScalar(prhs[0]); /* call the C subroutine */ func(z); }
Best Answer