假設我有一些功能foo
(不是我寫的)返回多個值,就像這樣:與調試多個返回值評估功能
function [one, two, three, four] = foo()
one = 2;
two = 4;
three = 8;
four = 16;
end
(注:以上僅僅是一個例子;在一般情況下,我無法控制功能foo
。)
此外,假設我正處於MATLAB調試會話的中間。
如果我現在評估foo
,只能先返回被顯示的值:
K>> foo()
ans =
2
如果我試圖捕捉所有的值與賦值表達式,我得到一個錯誤或其他;例如:
K>> all_returned_values = foo()
Attempt to add "all_returned_values" to a static workspace.
See Variables in Nested and Anonymous Functions.
K>> [v1 v2 v3 v4] = foo()
Attempt to add "v1" to a static workspace.
See Variables in Nested and Anonymous Functions.
K>> {v1 v2 v3 v4} = foo()
{v1 v2 v3 v4} = foo()
↑
Error: The expression to the left of the equals sign is not a valid target for an assignment.
有沒有辦法迫使MATLAB返回,做不依賴於分配功能的所有值?
注意:我正在尋找一種解決方案,不需要以任何方式修改foo
功能。 (這個函數可能不在我的控制之下;例如,它可能是一個內置的MATLAB函數。)
更好地使用'ANS =細胞(1,4); [答案{:}] = FOO()'。無論先前分配給ans的值如何,它都可以工作。 – Daniel
@Daniel好點。添加。 – Suever