有沒有一種方法來概括以下內容? (注:nargout_requested
可以不除已知在運行時)在matlab中調用一個函數,並讓調用者在運行時確定輸出參數的數量
function outputs = apply_and_gather(f, args, nargout_requested)
switch nargout_requested
case 0
f(args{:});
outputs = {};
case 1
o1 = f(args{:});
outputs = {o1};
case 2
[o1,o2] = f(args{:});
outputs = {o1,o2};
case 3
[o1,o2,o3] = f(args{:});
outputs = {o1,o2,o3};
...
換句話說,我想調用具有變元的單元陣列的功能,以及分配功能的輸出到單元陣列,並請求一定數量的輸出參數。
在蟒蛇,這將僅僅是:
outputs = f(*args)
但Matlab要求你,告訴你多少個參數要你調用之前的函數,如果你有太多的輸出參數給你一個錯誤。
我想你也可以避免傳遞'nargout_requsted'並使用內置的['nargout'](http://www.mathworks.com/help/matlab/ref/) nargout.html)而不是 – Will
也許我沒有在我的問題中說清楚:我有理由手動指定我想要的參數數量。這是用於測試用具。 –
'apply_and_gather'函數的作用是破壞MATLAB的正常多重參數方法,並將結果作爲單元格數組返回。 'apply_and_gather'的'nargout'值始終爲1. –