How to get multiple outputs from a function?

Hi, I'm using the latest version of MATLAB and trying to get 2 mulitple outputs from the simple function below.
function [x,y] = subfuntest(a,b)
x = a - b; y = a + b;
end
After I save it to an m-file, I typed subfuntest(1,2) in the command window and it only shows -1, which is the outcome of x.
How can I get both x and y, instead of just get x?

 Accepted Answer

[x,y] = subfuntest(a,b);
When you call it with no arguments, it returns only the first, putting it in the default variable name of ans, then dumping the other arguments into the bit bucket.

8 Comments

Thx for your answer but I can't get it:( I googled for the problem before I left this question here, and I found this: https://www.mathworks.com/matlabcentral/answers/14116-multiple-function-outputs
I think the accepted answer of the link is saying similar words like yours. Would you plz show any specfic function code example?
I DID SHOW IT!!!!!!!!!! I DID GIVE YOU CODE.
When you use ANY function in MATLAB, if you just use it with no output arguments, what does it do??? READ WHAT I SAID.
x = 1:3;
>> mean(x)
ans =
2
>> max(x)
ans =
3
Both calls dumped the answer to the command line, and called it ans. Do you see that? There will now be a variable in your workspace called ans.
Usually, when you use function, you put the result into some variable, named what you want.
fred = max(x)
fred =
3
>> whos
Name Size Bytes Class Attributes
ans 1x1 8 double
fred 1x1 8 double
x 1x3 24 double
As you can see, ans is still there, but now we have fred too.
However, if you read the help for max, you will find that it has TWO outputs that it can return. When you want to see the other outputs of a function, you must assign them to some variable name of your choice. (You can also call them exactly what they are in the documentation for the function, or when you created the function itself.)
[fred, barney] = max(x)
fred =
3
barney =
3
whos
Name Size Bytes Class Attributes
ans 1x1 8 double
barney 1x1 8 double
fred 1x1 8 double
x 1x3 24 double
Do you see that, now a new variable exists in your workspace called barney?
If you do not do that, then the second and later returns from a function are just dumped into the bit bucket, as I said.
Now, how should you call your function to get the other arguments? READ WHAT I SHOWED IN MY ANSWER!
[x,y] = subfuntest(a,b);
Variables named x and y will now be returned to your workspace. Or fred, barney, wilma and pebbles, if you name them that.
I would strongly suggest that you need to read the basic getting started documentatino, where you would learn these things. In fact, the getting started docs will have lots of examples.
Oh I'm sorry for you to write the long, detailed example. Thx to you, I could understand it finally:)
@Jongwu Kim: It belongs to the nature of learning (e.g. a programming langauge), that difficulties look childish for someone, who is professionally familiar with this topic. Sometimes this causes emotions on both sides.
Your questions about Matlab are welcome in this forum. It is a good idea to use the helping and businesslike parts of the answers. If the other parts don't help you to master Matlab, it might be a reasonable idea to shrug your shoulders.
@John D'Errico No, you didn't answer. You basically copied his line of code, which still doesn't solve the problem.
@Haroun Saaid: Please read the answer carefully again.
The definition of the function is:
function [x,y] = subfuntest(a,b)
x = a - b; y = a + b;
end
To obtain the two outputs, you have to call this function catching both:
[x,y] = subfuntest(a,b)
Yes, this is the 1st line of the definition of the function omitting the keyword "function". This is how Matlab works and therefore John has posted the working solution.
@Jan: Thank you so much. I'm in a matlab course right now and your clarification has helped me a lot on my midterm.
@Jan Thank you so much for your clarification

Sign in to comment.

More Answers (1)

Retrieving two outputs from a function requires two steps.
  1. Define the function to return two outputs
  2. Call the function with two outputs
You've performed the first of those steps. Your function definition states that it returns two outputs, the contents of the variables x and y created inside your function.
function [x,y] = subfuntest(a,b)
Now you (or really the user of your code, which could be you or could be someone with whom you share the code) need to perform the second step: call the function with two outputs.
[abra, cadabra] = subfuntest(1, 2)
Calling it with one output would only get the first input (unless the function author took specific steps to make it behave otherwise. But that's a little more of an advanced maneuver. You didn't take those steps in your code.)
hocuspocus = subfuntest(1, 2) % hocuspocus has the same contents as abra
Calling it with zero outputs is like calling it with one output that is named ans.
subfuntest(1, 2) % ans has the same contents as abra and hocuspocus

3 Comments

I'm grateful to your effort too:D
What if you only have 1 input variable but want 2 outputs?
As long as the function is defined to accept one or more input variables (and be callable with just one input) and return two or more outputs (and be callable with just two outputs) that's fine.
callWithOne = squareAndCube(1:5)
callWithOne = 1×5
1 4 9 16 25
[callWithTwo1, callWithTwo2] = squareAndCube(1:5)
callWithTwo1 = 1×5
1 4 9 16 25
callWithTwo2 = 1×5
1 8 27 64 125
function [x2, x3] = squareAndCube(x)
% This function accepts up to one input argument.
% This function REQUIRES at least one input argument.
%
% This function returns up to two output arguments.
%
% The first output is the square of the input.
% The second output is the cube of the input.
x2 = x.^2;
x3 = x.^3;
end

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!