How to write my function output in various form?
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
monkeyquant
am 18 Mär. 2022
Kommentiert: Jan
am 19 Mär. 2022
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
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (2)
Davide Masiello
am 18 Mär. 2022
Bearbeitet: Davide Masiello
am 18 Mär. 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 Kommentare
Stephen23
am 18 Mär. 2022
Bearbeitet: Stephen23
am 18 Mär. 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"
[m,~] = size(A)
[~,n] = size(A)
[m,n] = size(A)
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)
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.
Jan
am 18 Mär. 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()
Siehe auch
Kategorien
Mehr zu Argument Definitions finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!