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
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!