【发布时间】:2016-07-05 02:57:41
【问题描述】:
假设我有一些函数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 函数。)
【问题讨论】:
标签: matlab