Is there a way to have one function that can return two different variables, but only one at a time AND knowing which one is returned in the function call?
example: I have the following function in which only one of the outputs is valid (the other one would be [])
function [a,b] = AlternatingOutput (input)
if input == 1
return ONLY A
else
return ONLY B
end
end
and i call it in a script
[a,b] = AlternatingOutput (input)
i want a way to say the following (pseudocode):
if (function outputs a)
[a,~] = AlternatingOutput(input)
elseif (function outputs b)
[~,b] = AlternatingOutput(input)
end
the script is run in a loop, and later i need the newest Valid values for a and b, so i cannot overwrite one of the two with []
I do understand that I could just write a function that checks which variable will be output, but I was wondering if there is a more elegant way.
I hope I have made my question clear, and I hope someone can answer me :)
AlternatingOutputreturn values to temp names:[ax,bx] = AlternatingOutput(input), and reassign them only when returned value is not sentinel (if ax ~= sentinel a = ax; end. Or reconsider your function I/O, it honestly smells like bad design.[]may be used as sentinel value.