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

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by