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

1 ビュー (過去 30 日間)
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 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 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 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 件のコメント表示非表示 1 件の古いコメント
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).

サインインしてコメントする。

### Community Treasure Hunt

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

Start Hunting!