How to write my function output in various form?

9 views (last 30 days)
Many MATLAB functions have options to spit out various forms of outputs. I have a function spitting out 7 arrays. I would like to use either a single output as a matrix composed of all 7 arrays or arrays by specifying each array by name using something like [~,~, a, b, ~, ~, ~]. Any advice or references?
Some functions with a single output allocate the first output in the list of [,...,], and I would like to get all outputs with a single output assignment without the square bracket, [], and each output by utilizing the square bracket.
For example,
>> A=magic(3);
>> size(A)
ans = 3 3
>> [m, ~] = size(A)
m = 3
>> [~,n]=size(A)
n = 3
>> [m, n] = size(A)
m = 3
n = 3

Accepted Answer

Stephen23
Stephen23 on 18 Mar 2022
Edited: Stephen23 on 18 Mar 2022
This is how to do achieve what you asked for:
function [out1,out2,out3,out4,out5,out6,out7] = yourFunction(..)
..
out1 = ..;
..
out7 = ..;
..
if nargout<2
out1 = [out1,out2,out3,out4,out5,out6,out7];
end
end
  2 Comments
monkeyquant
monkeyquant on 18 Mar 2022
You already shared this with me. Appreciate millions!
Jan
Jan on 19 Mar 2022
@monkeyquant: Does this solve your problem? Then please accept it.

Sign in to comment.

More Answers (2)

Davide Masiello
Davide Masiello on 18 Mar 2022
Edited: Davide Masiello on 18 Mar 2022
Simply, your function should look like this
function [out1,out2,out3,out4,out5,out6,out6] = yourFunction(input)
...
...
...
out1 = first array;
out2 = second array;
etc.
end
Then, assuming you want your seven arrays outputted by you function be names "a,b,c,d,e,f,g", you call you function this way
[a,b,c,d,e,f,g] = yourFunction(input);
If at some point you want your functoin to return, say, a and f, you call the function this way
[a,~,~,~,~,f,~] = yourFunction(input);
To have the output as a single matrix, all the arrays must have same size. The you can write your function as
function out = yourFunction(input)
...
...
...
out = [first array;second array; third array;..];
end
  4 Comments
Stephen23
Stephen23 on 18 Mar 2022
Edited: Stephen23 on 18 Mar 2022
"The matlab built-in size does that."
No, it does not, it never has, and it probably never will:
A = rand(4,3,2);
S = size(A) % this is NOT "all of the outputs joined into one array"
S = 1×3
4 3 2
[m,~] = size(A)
m = 4
[~,n] = size(A)
n = 6
[m,n] = size(A)
m = 4
n = 6
SIZE can return an unlimited number of outputs, so your incorrect understanding of how its works is difficult to interpret: should the one "joined" output actually be an infinitely long vector?
[a,b,c,d,e,f,g,h,i,j,k,m,n,o] = size(A)
a = 4
b = 3
c = 2
d = 1
e = 1
f = 1
g = 1
h = 1
i = 1
j = 1
k = 1
m = 1
n = 1
o = 1
Your understanding that the output S is caused by "joining" of all of the outputs is incorrect: the outputs of every function are solely determined from within the function itself. In SIZE's case (and with some other functions ) the outputs change depending on the number of requested outputs (as my example above demonstrates), which you confused with some kind of automagic concatenating of all outputs (but in reality this does not occur).
You need to do this from inside the function (see NARGOUT), not by attempting to use a behavior of MATLAB that does not exist. Or use a comma-separated list into a cell array.
monkeyquant
monkeyquant on 18 Mar 2022
Great to save my time. Just one more thing from what you mentioned of “NARGOUT”. So it is possible by counting the number of outputs or syntax with/without []. I would appreciate if you could direct me to an example.

Sign in to comment.


Jan
Jan on 18 Mar 2022
This works automatically with any function. The ~ simply ignores the replied value in the caller. You do not have to consider this in the function.
If you function replies 7 output, you can request the 3rd and 4th by:
[~, ~, a, b] = YourFunction()

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!