How to get multiple outputs from a function?

2,049 ビュー (過去 30 日間)
Jongwu Kim
Jongwu Kim 2020 年 1 月 10 日
コメント済み: Jhon Onill 2023 年 3 月 5 日
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?

採用された回答

John D'Errico
John D'Errico 2020 年 1 月 10 日
編集済み: John D'Errico 2020 年 1 月 10 日
[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 件のコメント
Eric
Eric 2022 年 10 月 26 日
@Jan: Thank you so much. I'm in a matlab course right now and your clarification has helped me a lot on my midterm.
Jhon Onill
Jhon Onill 2023 年 3 月 5 日
@Jan Thank you so much for your clarification

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2020 年 1 月 10 日
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 件のコメント
John Redmond
John Redmond 2021 年 6 月 29 日
What if you only have 1 input variable but want 2 outputs?
Steven Lord
Steven Lord 2021 年 6 月 29 日
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by