12

Lets say I have a function:

function [ A, B, C ] = test(x, y, z)
    A=2*x;
    B=2*y;
    C=2*z;
end

When you press run, Matlab returns only the first value from the output arguments - [A] in this case. Is there a command that I can put inside my function that automatically returns all the function output arguments [A,B,C] instead of just the first argument. I know I can type in my command windows [ A, B, C ] = test(x, y, z) and get all the values, but I am lazy sometimes, and would just like to press Run and get automatically all the values.

4 Answers 4

9

Some options:

Add a parameter to specify verbose output the console but set it to false by default:

function [ A, B, C ] = test(x, y, z, verbose)

   if nargin = 3
       verbose = false;
   end;

   A=2*x;
   B=2*y;
   C=2*z;

   if verbose
       fprintf('A = %f\nB = %f\nC = %f', A, B, C);
   end;

end

or combine them into one output:

function output = test(x, y, z)

   A=2*x;
   B=2*y;
   C=2*z;

   output = [A, B, C]; %// Or {A;B;C} if they're not going to be the same size, but then it won't display anyway

end

or if you really really want to I guess you could write a wrapper function that you call on your function and it displays all three for you that you could use generically on any function. But that hardly seems worthwhile.

Sign up to request clarification or add additional context in comments.

1 Comment

If you use {A;B;C} you can display them by adding A=output{1}, B=output{2} and C=output{3} to the end of the code, without semicolons.
4

Another option is to use assignin to automatically save an output argument to the workspace

function [ A, B, C ] = test(x, y, z)
    A=2*x;
    B=2*y;
    C=2*z;

    assignin('base', 'A', A);
    assignin('base', 'B', B);
    assignin('base', 'C', C);      
end

'base' is the name of the main workspace used when you call variables from the command window.

This way you can type test(x,y,z) into the workspace without the [A,B,C] = part and it will still give you all the values.

The benefit of this over combing A, B and C into one output is that you will still have 3 seperate variables saved in your workspace. This is useful if A, B and C are arrays or cells. A disadvantage of this method is that if you use this function inside another function it will still only use the value of A. For example: length(test(x,y,z)) will just give the length of A.

Comments

1

Matlab function outputs are in cell format, so you can define a cell data with the size same as the function output and use it as a single variable to store all the outputs in a more structured way :)

a = cell{3, 1};
[a{:}] = test(x, y, z);
A = a{1};
B = a{2};
C = a{3}; 

6 Comments

Matlab function outputs are in cell format, so you can define a cell data with the size same as the function output and use it as a single variable to store all the outputs in a more structured way :)
wshan, thanks for your quick response. I'm not aware of matlab at all, but you can edit your answer, and update it with the as much info as you have. This will make this answer more clear and understandable to the other users. However I'll give my +1 for your answer if you can improve your answer.
Just make sure, your answer don't duplicate other (existing) answers. Or if you want to give more better answer (should relevant to the question) then explain why this answer is a more better answer.
If I can suggest something, use deal: [A, B, C] = deal(a{:});.
first line should be a=cell(3,1); Code is perfect
|
0

MATLAB will automatically ouput the variables/expressions those are not end with a ';'.

So if you just need to display all these values, the simplest way will be:

function [ A, B, C ] = test(x, y, z)
    A=2*x    % no ';' will print A's value automatically
    B=2*y    % no ';' will print B's value automatically
    C=2*z    % no ';' will print C's value automatically
end

2 Comments

The only issue is that this will always print the values, even when you don't want them.
@Dan Yes. But suitable for OP to output all values. :P

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.