How to define a criteria for output arguments of a function?
1 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
Voss
on 11 Jan 2023
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 Comments
More Answers (0)
See Also
Categories
Find more on Get Started with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!