MATLAB: Need help on assigning output arguments in C language

cmex

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:N
mwIndex i;
int randNo;
int *haveRand; // pointer to haveRand
haveRand=malloc(sizeof(int) * N); // allocate memory to haveRand
for(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 k
void 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

Defining functions inside of functions is an extension to the language. In your posted code, that means the functions compile OK with MinGW (i.e., GCC) but throw errors in other compilers such as MSVC. Given this, the mexFunction in your posted code has no body (other than the function definitions), so no plhs[] variables are produced. This generates an error at the caller level when trying to assign an output since no output was produced by the mex function. See this discussion:
Your posted code is basically equivalent to this do nothing function:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){}