how to make a mex 'answer' nargin/nargout
이전 댓글 표시
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).
답변 (4개)
Walter Roberson
2011년 12월 29일
0 개 추천
"Enclose function_name in single quotation marks."
But I suspect that will not solve your problem.
James Tursa
2011년 12월 30일
0 개 추천
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.
Friedrich
2011년 12월 30일
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.
Jan
2011년 12월 30일
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.
카테고리
도움말 센터 및 File Exchange에서 Write C Functions Callable from MATLAB (MEX Files)에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!