2

I have wondered for sometime now if it is possible to write a function in Matlab that does not return an argument if none is requested. For example does plot returns the axis handle if it is called as h = plot(x,y);, but not if it is called as plot(x,y);. It is just that plot is not written in Matlab.

3 Answers 3

2

A short example to demonstrate how to get a flexible number of random outputs:

function varargout=myFancyOutput()
    varargout=cell(1,nargout)
    for idx=1:numel(varargout)
        varargout{idx}=rand
    end
end

nargout gives the number of output arguments and varargout is expanded to a list of individual outputs.

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

Comments

2

You can do it using varargout, for example:

function varargout = test()

    for k = 1:nargout
        varargout{k} = k;
    end

    disp(['Test function called with ', str2num(nargout), ' output arguments']) %// Just so you can see the function working when you call no outputs

end

2 Comments

Ok since you was first I will accept your answer. I actually managed to find it out by myself before :). Thank you anyway
Actually I think Daniel was a few seconds ahead of me ;)
1

Actually when a function returns an output you can just choose not to store it.

Let a function be:

function [out1,out2,out3] = testfunction(x,y)
    out1 = 2.*x;
    out2 = 2.*y;
    out3 = x.*y;
    plot(1:10,1:10)
end

Now you can call this function using:

% Call function interested in no output
testfunction(2,3); % Although in this case `ans` is generated as pointed out by Dan.
% Call function only interested in first output
[out1] = testfunction(2,3)
% Call function only interested in first and second output
[out1,out2] = testfunction(2,3)
% Call function only interested in first, second and third output
[out1,out2,out3] = testfunction(2,3)
% Only the first output
[~,~,out3] = testfunction(2,3)

Depending on the assignment of the output the output is generated.

A very dirty work-around to avoid ans being generated is:

[~]=testfunction(2,3)

This avoids the extra coding telling how much output arguments are needed.

7 Comments

+1 You should include a test case that is just testfunction(2,3) with no output arguments at all. This should still work and just assigns your first output, out1 to ans. However if you want no output at all (i.e. nothing to ans), then you'll need to use varargout
Well you will still have a problem with testFunction(1,2) returning something. Using the varargout-nargout property, you can let the call testFunction(1,2) be a void function.
@Dan Or just call [~] = testfunction(2,3), no need for nargout really.
@patrik I don't know whether calling with [~]=testfunction(1,2) is a true void or that the output is assigned to a temporary variable and is immediately destroyed afterwards, but it works fine anyway (at least on my version of MatLab)
@EJG89 Right the problem is rather the when you do not want an output then you want to do a call testfunction(1,2) and nothing are going to be returned. With the [~] syntax you especially asks for returning nothing and that is what I tried to get rid of. It is intuitive to only return a value when you request it. This is how it is done in most programming languages, except that most languages generate a compilation error if no return value is assigned. However, then you have void functions instead, which anyway is more complicated than matlabs solution. Anyway thanks for your input.
|

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.