假设我有一些功能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