How can I solve a problem related to the output argument.

조회 수: 2 (최근 30일)
Fidele Adanvo
Fidele Adanvo 2020년 11월 18일
댓글: Star Strider 2020년 11월 18일
% I will make the code simpler. Let's assume I have this code. And depending on the input value I want to show assign a=10 and b=5 different values (disregard the %value of the output C) or assign c=100 a value and disregard a and b.
function [a,b,c,d] = trial_function(num)
if num==2
a=4;
b=5
elseif num==4
c=100;
d=230
end
%I am getting an error. Output argument "b" (and maybe others) not assigned during call to
%Please, can you help me.Thank you.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 11월 18일
편집: Ameer Hamza 2020년 11월 18일
The name of output argument does not matte outside the function. If you just want to output two values for each case, just use
function [a,b] = trial_function(num)
if num==2
a=4;
b=5;
elseif num==4
a=100;
b=230;
end
Regardless, If you want to output 3rd and 4th argument in 2nd case, then you can set a and b to empty vectors
[~,~,x,y] = trial_function(4)
function [a,b,c,d] = trial_function(num)
if num==2
a=4;
b=5;
elseif num==4
a = [];
b = [];
c=100;
d=230;
end
end
If you want to output variable number of output arguments, then use varargout: https://www.mathworks.com/help/matlab/ref/varargout.html
  댓글 수: 1
Fidele Adanvo
Fidele Adanvo 2020년 11월 18일
Thank you so much for everything.
Assume that it is a very long code and that the values of a, b, c, and d can be requested at some point.
The first solution does not help me much because it controls the behavior of the 4 variables.
As for the second, if I call the function "trial_function", and he enters the 'elseif' according to this answer, a == []. and b = []. (remove the values it had)
But I want to achieve is to keep the value that a and b had to perform another task with the inside code.
Will you have a solution that does not modify the value of the variable a and b?

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

추가 답변 (1개)

Star Strider
Star Strider 2020년 11월 18일
The way I usually deal with this is to define all the outputs as NaN in the beginning of the function. The ones that are assigned in the function will overwrite tha NaN value, and it is also easy to determine the values that were not assigned.
Example —
function [a,b,c,d] = trial_function(num)
[a,b,c,d] = deal(NaN);
if num==2
a=4;
b=5
elseif num==4
c=100;
d=230
end
end
.
  댓글 수: 2
Fidele Adanvo
Fidele Adanvo 2020년 11월 18일
Let's suppose that in very large code.
I call the function "trial_function", and he enters the 'elseif' according to this answer, a == NaN. and b = NaN.
But I want to achieve is to keep the value that a and b had to perform another task with the inside code.
Will you have a solution that does not modify the value of the variable a and b?
thank you
Star Strider
Star Strider 2020년 11월 18일
In my approach, the outputs are assigned NaN until they are assigned other values in the if blocks. If they are not assigned other values, they remain NaN. Assign them any value you wish, then modify that value later in the code.
Assigning them a specific value (whether NaN, 0, or something else) avoids throwing the warning or error if the values are not assigned in the if blocks. It will also indicate which values are not assigned, since those will be NaN (or any other default value).

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by