多個輸出
以下 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