Need help on assigning output arguments in C language

3 visualizzazioni (ultimi 30 giorni)
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
  6 Commenti
OCDER
OCDER il 13 Giu 2018
Also, there is no reference to plhs[x], which means no output was ever assigned. A word search in the code never finds plhs besides in the mexFunction gateway.
/* The gateway function */
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]) {
//ERROR: No use of plhs! No output was assigned to this function.
//WARNING: No use of prhs! No input is needed or referred to in this function
}
Guillaume
Guillaume il 13 Giu 2018
Modificato: Guillaume il 13 Giu 2018
In addition to the problems pointed out by Jan, I'm fairly certain that:
double pLength = sizeof(p)/sizeof(p[0]);
is wrong. That syntax only works when p is an array, not a pointer. There is no way to retrieve the size of the original array from within generateRandSumc. It needs to passed as an argument. Thankfully, pLength is never used so the incorrect calculation doesn't matter. As it is pLength will always be 1 on a 64-bit machine.
If mex generateRandSumc.c compiles succesfully, there's no way that it's compiling the code posted above. It must be compiling a different file in another location. One that is at least valid C.
As Steven says, why bother with the mex code if you've got working matlab code. At least, the matlab code is reliable and does not have the bias exhibited by the C code.

Accedi per commentare.

Risposta accettata

James Tursa
James Tursa il 13 Giu 2018
Modificato: James Tursa il 13 Giu 2018
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[]){}

Più risposte (1)

Tero
Tero il 13 Giu 2018
Thanks all for taking the time.
It seems the code has more than enough issues to solve, which means I will follow Steven's argument and continue using the m-function. Having the function in C does not gain me benefit that would overcome the work needed here.
Thanks again, Tero

Categorie

Scopri di più su Write C Functions Callable from MATLAB (MEX Files) in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by