Hi all,
I have a simple function written in C, which I've been using as a MEX. In the earlier versions of Matlab, it has been working ok, but the current release (and couple of earlier ones) crashes eventually when attempting to run the code. By compiling the C into a MEX in a current release, and then running it, gives out the error "one or more output arguments not assigned…". So, a simple problem it seems, but I'm not that familiar with C, so any help is much appreciated.
Below is a screenshot showing how I'm able to run the old MEX in a current release couple of times until Matlab eventually crashes:
The function "generateRandSumc" returns a vector of random integers, where the length of the vector is the first argument (15 in the example), and the sum of the vector is the second argument (48).
Below is a second screenshot that shows how the current release can detect the missing output arguments after the MEX compilation:
And finally, here is the C-code: (sorry for the not-so-good-structuring)
#include "mex.h" #include "matrix.h" /* The gateway function */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){ // random number sequence picked from 1:k, of size n void randpermc(mwSize N, mwSize k, double *p) {// N is the max range of the results in the array// k is the length of the array// i.e., select k values in the range 1:NmwIndex i;int randNo;int *haveRand; // pointer to haveRandhaveRand=malloc(sizeof(int) * N); // allocate memory to haveRandfor(i=0; i<=N; i++) haveRand[i] = 0; // initialize array. for(i=0; i<k; i++) { do { randNo = rand()%N +1;
} while ( haveRand[randNo] == 1 ); haveRand[randNo] = 1; p[i]=randNo; }} int cmp(const void *x, const void *y){double xx = *(double*)x, yy = *(double*)y;if (xx < yy) return -1;if (xx > yy) return 1;return 0;} // random number sequence picked from 1:n, of size kvoid generateRandSumc(mwSize n, mwSize summ, double *p){int i;double pLength = sizeof(p)/sizeof(p[0]);double *q;q=malloc(sizeof(double) * n); // allocate memory randpermc(n+summ-1,n-1,q); qsort(q, n-1 ,sizeof(double), cmp); for(i=0; i<n; i++) { if ( i == 0 ) { p[i]=q[i]-1; } else if ( i == n-1 ) { p[i]=n+summ-q[i-1]-1 ; } // add endpoint else { p[i]=q[i]-q[i-1]-1 ; } } }}
I hope this makes some sense
Thanks, Tero
Best Answer