필터 지우기
필터 지우기

How to write my function output in various form?

조회 수: 10 (최근 30일)
monkeyquant
monkeyquant 2022년 3월 18일
댓글: Jan 2022년 3월 19일
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
monkeyquant
monkeyquant 2022년 3월 18일
You already shared this with me. Appreciate millions!
Jan
Jan 2022년 3월 19일
@monkeyquant: Does this solve your problem? Then please accept it.

댓글을 달려면 로그인하십시오.

추가 답변 (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