Evaluating functions with multiple return values ​​in the debugger

Suppose I have some function foo(not written by me) that returns multiple values, for example:

function [one, two, three, four] = foo()
    one = 2;
    two = 4;
    three = 8;
    four = 16;
end

(NB: the above is just an example, in general, I do not control the function foo.)

Also, suppose I'm in the middle of a MATLAB debugging session.

If you evaluate now foo, only the first of the returned values ​​is displayed:

K>> foo()
ans =
     2

If I try to capture all the values ​​using an assignment expression, I get one error; eg:

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.

Is there a way to force MATLAB to return all function values ​​that are independent of assignment?

NB: , foo. ( , , MATLAB.)

+4
2

ans , - , .

% Force ans to be a cell first
ans = cell();

% Assign all outputs to elements in ans
[ans{1:4}] = foo()

ans , foo. ans{1:4} , , .

, ans.

disp(ans{1})  % rather than ans{1} with no semicolon

% Alternately
celldisp(ans)

, , nargout .

[ans{1:nargout('foo')}] = foo();
+4

: Matlab 2013b , , , . , , Matlab 2015b. , .

. :

: MATLAB

. , , K>> global X;
K>> X = myvalue;

, , , . .

, :

K>> global v1 v2 v3 v4;
K>> [v1, v2, v3, v4] = foo();
+2

All Articles