How to define a criteria for output arguments of a function?

1 回表示 (過去 30 日間)
Kalasagarreddi Kottakota
Kalasagarreddi Kottakota 2023 年 1 月 11 日
回答済み: Voss 2023 年 1 月 11 日
Consider a sample code schematic as shown below: The following function has 2 options, 1st option will deliver 2 output argurments(d,e) and second option delivers only 1 output argument (f). So what shold I write in the function(........? here?), so that if option 1 is selected 2 outputs are returned and when option 1 is selction only 1 output is returned.
function() = operation(a,b,c,'option')
if option ==1
d = a+b;
e= b+c;
end
if option == 2
f = a+b+c
end
end

採用された回答

Voss
Voss 2023 年 1 月 11 日
One way:
function [d,e] = operation(a,b,c,option)
if option == 1
d = a+b;
e = b+c;
end
if option == 2
d = a+b+c;
e = [];
end
end
Another way:
function varargout = operation(a,b,c,option)
if option == 1
d = a+b;
e = b+c;
varargout = {d,e};
end
if option == 2
f = a+b+c;
varargout = {f};
end
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by