Why doesn't evalin('ca​ller','nar​gout') work?

2 ビュー (過去 30 日間)
Matt J
Matt J 2022 年 12 月 1 日
コメント済み: Bruno Luong 2022 年 12 月 1 日
Why doesn't the mynargout() function correctly return n=3? How can one programmatically evaluate nargout in the calling function?
[A,B,C]=Func();
function varargout=Func
varargout={1,2,3};
nargout
mynargout
end
function n=mynargout()
n=evalin('caller','nargout');
end

採用された回答

Bruno Luong
Bruno Luong 2022 年 12 月 1 日
編集済み: Bruno Luong 2022 年 12 月 1 日
Obviously it is NOT a normal "function", according to the doc of nargout
"nargout returns the number of function output arguments specified in the call to the currently executing function. Use this syntax in the body of a function only."
Therfore the nargout won't be propagae by evalin but returns the result corresponds number of the output to the last function in the stack meaning mynargout
[A,B,C]=Func();
a = 3
b = 1
function varargout=Func
varargout={1,2,3};
a=nargout
b=mynargout
end
function n=mynargout()
n=evalin('caller','nargout');
end
  2 件のコメント
Matt J
Matt J 2022 年 12 月 1 日
Use this syntax in the body of a function only.
I''m sure that's not what they meant when they wrote that. They mean you can't invoke nargout at the command line or in a script:
nargout
Error using nargout
You can only call nargin/nargout from within a MATLAB function.
Anyway, it looks like it's not possible.
Bruno Luong
Bruno Luong 2022 年 12 月 1 日
Yes it could be interpreted like that as well.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by