0

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 :)

3
  • 2
    Always return two values, but use a sentinel value for "not-returned" value. Retrieve AlternatingOutput return 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. Commented Jul 1, 2016 at 11:16
  • 2
    just put a flag instead of the second output , if flag is 1 output == a if flag is 0 output b Commented Jul 1, 2016 at 11:18
  • Oh, and [] may be used as sentinel value. Commented Jul 1, 2016 at 11:21

4 Answers 4

4

There is no way to tell if an output argument is actually used. You may check the number of output arguments using nargout and it would allow to distinguish between [a] = AlternatingOutput(input) and [~,b] = AlternatingOutput(input)

I don't know the full context of your problem, but maybe you can put all your variables into a struct? Simply pass this struct everytime you call the function and let it decide which variables to manipulate. (This might be slow in some programming languages, but not in matlab).

Sign up to request clarification or add additional context in comments.

Comments

0

How about retuning a cell?

function [ ab ] = testfun( input )
if input
    ab={'ax'};
else
    ab={2};
end
end

No worries about what is in the cell.

thb you could return what ever you want, Matlab does not check the type anyways

Comments

0

If only one of the outputs from the function AlternatingOutput is valid, then you only need to return one output:

function [X] = AlternatingOutput(input)
    if input == 1
        X = A; 
    else
        X = B;
    end
end

To allocate the retured value to either a or b in the loop, put them into a cell:

C = {AlternatingOutput(1), AlternatingOutput(2)};

and then use input to determine which value is change. If input is either 1 or 2 you can just do

for counter = ...
    input = mod(input,2)+1;
    C{input}=AlternatingOutput(input);
end

Comments

0

If your function doesn't mind accepting more input variables, why not pass a and b as input:

function [a,b] = AlternatingOutput(a,b,input)
    if input == 1
        a = new_value_for_a;
        % b retains its former value
    else
        % a retains its former value
        b = new_value_for_b;
    end
end

Then it can be easily called from your script in a loop:

for i= ...
    [a,b] = AlternatingOutput(a,b,input);
    ...
    ...
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.