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
0 个评论
采纳的回答
更多回答(2 个)
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
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"
[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
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 Center 和 File Exchange 中查找有关 Argument Definitions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!