多个输出
以下 MATLAB 脚本显示了如何在单个函数中返回多个输出:
myFun.m :
function [out1, out2, out3] = myFun(arg0, arg1)
out1 = arg0 + arg1;
out2 = arg0 * arg1;
out3 = arg0 - arg1;
end
终端 :
>> [res1, res2, res3] = myFun(10, 20)
res1 =
30
res2 =
200
res3 =
-10
但是,当分配给单个变量时,MATLAB 将仅返回第一个值
>> res = myFun(10, 20)
res =
30
以下示例显示如何获取特定输出
>> [~, res] = myFun(10, 20)
res =
200