4

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

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 have no control over the function foo.)

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

If I now evaluate foo, only the first of the values it returns gets displayed:

K>> foo()
ans =
     2

If I try to capture all the values with an assignment expression, I get one error or another; for example:

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 the values of the function that does not rely on an assignment?

NB: I'm looking for a solution that does not require modifying the function foo in any way. (This function may not be under my control; e.g., it could be a built-in MATLAB function.)

2 Answers 2

4

You can always add ans to the static workspace so you could do something like this to get all output values.

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

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

This forces ans to be a cell array where the first four entries will be filled with the outputs of foo. ans{1:4} creates a comma-separated list which will automatically expand to be four output arguments.

Be careful accessing the resulting cell array because thing will automatically get assigned to ans.

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

% Alternately
celldisp(ans)

If you want this to be more flexible, you could use nargout to determine the number of output arguments dynamically.

[ans{1:nargout('foo')}] = foo();
Sign up to request clarification or add additional context in comments.

1 Comment

Better use ans=cell(1,4);[ans{:}] = foo(). It works regardless of the value previously assigned to ans.
2

WARNING: The following works in Matlab 2013b and, presumably, earlier versions. However it appears that it no longer works in Matlab 2015b. It is therefore an issue with documentation, which is not up to date.

This question is specifically addressed in the documentation. Here's the best practice advice from Variables in Nested and Anonymous Functions guide:

Type of Operation: Assigning to a variable in the MATLAB debugger

Best Practice to Avoid Dynamic Assignment: Create a global variable for temporary use in debugging, such as
K>> global X;
K>> X = myvalue;

Note that it only affects scopes of anonymous functions, nested functions, or functions that contain a nested function. In all other cases you should be fine doing a simple assignment.

So in your case, the following would work:

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

2 Comments

Have you actually tried this? When I try the code you propose in the MATLAB debugger, I get one of the errors I mentioned in my post.
I have. It works just fine in 2013b, but does not work in 2015b. Apparently documentation is not up to date. Made it clear in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.