how to make a mex 'answer' nargin/nargout
3 views (last 30 days)
Show older comments
I wrote a mex function, call it foo When I try to get the nargout of this function, I get an error:
nargout(foo)
??? Error using ==> nargout
foo does not know how to answer nargin/nargout.
The form of this error makes me suspect there is some way to make foo 'answer' this question. I have not found such a way in my documentation.
edit: I experimented with a mex function distributed by the MathWorks, histc , and I still get errors (follow along at home):
>> nargout(@histc)
??? Error using ==> nargout
histc does not know how to answer nargin/nargout.
>> nargout('histc')
??? Error using ==> nargout
histc does not know how to answer nargin/nargout.
I presume if anyone could make mex behave properly in this regard, it would be the MathWorks (or maybe Yair Altman).
0 Comments
Answers (4)
Walter Roberson
on 29 Dec 2011
"Enclose function_name in single quotation marks."
But I suspect that will not solve your problem.
James Tursa
on 30 Dec 2011
You could have a companion m-file with the same base name, foo.m, with the same signature as the mex routine, and then use nargout('foo.m'). Not a great solution, but the only one I can think of at the moment. As a side comment, companion m-files are also good to hold the help information for the mex routine, and to use to auto-build the mex routine.
0 Comments
Friedrich
on 30 Dec 2011
I think this expected behavior and is totally okay. Each mexFunction has the same function signature:
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
You can’t determine from this signature how many in an output this mexFunction will have. In addition the source code determines how many output the mexFunction has and a mexFunction behaves like a MATLAB function with varargout as return value,
function varargout = bla(in1,in2,…)
You can’t tell here how many output it will have. In order to tell it you have to parse the source code and even than, the amount of return values can vary, e.g
function varargout = bla(in1,in2,…)
varargout{1} = 2
if rand(1,1) > 0.5
varagout{2} = 2
end
This is the same behavior for mex file since during runtime of that mex file its determined how many outputs it will allocate and try to return.
0 Comments
Jan
on 30 Dec 2011
From help nargout:
nargout(FUN) returns the number of declared outputs for the
M-file function FUN
A Mex-file is no M-file, therefore nargout will not work. The same happens for built-in functions, try:
nargin('plot')
ans = 1
No, plot does not use 1 input argument. But plot is a built-in function and nargin fails. I think nargin(Fun) and nargout(Fun) have a limited use only.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!