MATLAB: Variable input using a flag

flagMATLABvariable output

I'd like to create a function that has a variable number of return arguments based on the value of a flag, having a fixed number of input parameters.
[out1, out2] = myFun(inp1, inp2, 'a');
[out1, out2, out3] = myFun(inp1, inp2, 'b');
I tried using varargout but haven't managed to successfully accomplish what I want to do.

Best Answer

MATLAB returns arguments based on demand, so you can simply define all three of them:
function [out1, out2, out3] = myFun(inp1, inp2)
out1 = 1;
out2 = 2;
out3 = 3;
and then call it with either two or three output arguments. Try it!