How to write my function output in various form?

8 次查看(过去 30 天)
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

采纳的回答

Stephen23
Stephen23 2022-3-18
编辑:Stephen23 2022-3-18
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 个)

Davide Masiello
Davide Masiello 2022-3-18
编辑:Davide Masiello 2022-3-18
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 个评论
Stephen23
Stephen23 2022-3-18
编辑:Stephen23 2022-3-18
"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 2022-3-18
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.

请先登录,再进行评论。


Jan
Jan 2022-3-18
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()

类别

Help CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by