Borrar filtros
Borrar filtros

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

2 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Voss
Voss el 11 de En. de 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

Más respuestas (0)

Categorías

Más información sobre Programming en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by