MATLAB:如何从另一个功能
问题描述:
访问功能的第n个输出。如果我有一个函数:MATLAB:如何从另一个功能
function [out1,out2,...] = functionName[in1,in2]
function code here
end
而另一功能
function[newout1,newout2] = newfunctionName[in1,in2]
[newout1]=out1+out2;
[newout2]=out2+out3;
如何去调用各种输出, OUT1,OUT2,OUT3等...
答
我不明白你的意思是“调用各种输出,out1,out2,out3等”,但要回答标题的问题,
为了访问任何函数的第n个输出,您必须首先调用该函数的名称nargout,然后将其输出插入预分配大小的单元格中。下面的示例代码,
n = 5;
nout = abs(nargout('functionName'));
if n>nout
error(['n must be lower or equal than ',num2str(nout)])
end
out = cell(1,n);
[out{:}] = functionName(in1,in2);
nth_output = out{n};
这可以在其路径中具有functionName
的任何函数内完成。
可能重复? http://*.com/q/39359410/6579744 – rahnema1
你可以从'newfunctionName'中调用'functionName'并获取输出。我真的不确定这里的问题是什么。 – beaker
@beaker如果'newfunctionName'是递归的,并且说'如果n = 1 newout1 = 5; newout2 = 10; else [newout1,newout2] = functionName(newfunctionName(n/2),** out2 **)'我将如何获得'out2' – Anon21304982