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.
Add a comment
|
3 Answers
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.
Comments
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
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
Dan
+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 varargoutpatrik
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.EJG89
@Dan Or just call [~] = testfunction(2,3), no need for nargout really.
EJG89
@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)
patrik
@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. |