Mex File. Too many output arguments

Hello
I have created a mex file which works fine when is called with only an output argument, but it gives me the next output when called with more than one:
??? Error using ==> mtimes Too many output arguments.
I manage the output arguments in the mex function like shown next:
void mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] ) { mxArray *Result0,*Result1,*Result2,....
....
Result0 = mxCreateDoubleMatrix(N,M,mxCOMPLEX);
ResultValuesRe0 = mxGetPr(Result0);
ResultValuesIm0 = mxGetPi(Result0);
Result1 = mxCreateDoubleMatrix(N,M,mxCOMPLEX);
ResultValuesRe1 = mxGetPr(Result1);
ResultValuesIm1 = mxGetPi(Result1);
Result2 = mxCreateDoubleMatrix(N,M,mxCOMPLEX);
ResultValuesRe2 = mxGetPr(Result2);
ResultValuesIm2 = mxGetPi(Result2);
....
....
plhs[0] =Result0; plhs[1] =Result1; plhs[2] =Result2;
}
I just do not get it. This is the way I created some mex files some time ago, and they worked out.
Why this error when calling with, for example, two output arguments?
The way I call it: [x,y] = function_mex(input_arguments)

2 Comments

How does the "mtimes" figure in to this? Does the code call the MATLAB mtimes, or did you call your code mtimes ?
I do not have a clue idea why this message on "mtimes". I do not call mtimes from the mex function. When I call the function with two outputs, it gives that message.
With only one output argument, the output is given by plhs[0]=Result0. However, if I change the code such as plhs[0]=Result1, the output changes accordingly (no error).
I have opted to divide the code and create 3 functions, each one with only one output argument, since I think I am far from knowing what is going on.
Thank you

Sign in to comment.

 Accepted Answer

A save method to create the outputs in the Mex-file is checking the value of nlhs:
switch (nlhs) {
case 3: plhs[2] = Result2; // fall through!
case 2: plhs[1] = Result1; // fall through!
case 1: // fall through!
case 0: plhs[0] = Result0;
default:
// Error message: Too many outputs...
}
For nlhs==0 the created output is copied to the variable ans implicitely. Therefore a distinction between 0 and 1 output is not required.
But: This does not explain the original problem, because even with 1 output the original code creates too many outputs.

2 Comments

I remember I added the next line:
mexPrintf("number output arguments %d \n",nlhs);
The result was always that nlhs is equal to 1, independently of the way I called the function. Another thing I do not understand why.
Under Matlab 2009a, Windows7/64, MSVC 2008 I get nlhs==0, if the Mex-function is called without inputs.

Sign in to comment.

More Answers (0)

Categories

Asked:

on 22 Dec 2011

Community Treasure Hunt

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

Start Hunting!