Kirill  Demkin
Kirill Demkin

Reputation: 11

MatLab function, variable output

function [ muln, varargout ] = my_mul( varargin )
%MY_MUL This function is used to multiply numbers. 
%   My_mul function multiplies array of entered numbers, and outputs single
%   solution.
%   For example: my_mul(12, 2, 3, 5) gives ans = 360

if nargout >=1
    disp('Error, wrong number of output arguments');
    varargout{1} = 0;
    return

end
if nargin <= 1
    disp('Error, small number of input argumnets');
    return
else
    muln = 1;
    for i = 1:nargin
        muln = muln*varargin{i};
    end
end
end

Hi, everyone, I'm just doing my assignment for uni and have a qiuck question. How can I make this function to give an error if it is called with more than one output.(It meant to give only one) Thanks!

Upvotes: 1

Views: 288

Answers (2)

patrik
patrik

Reputation: 4558

My opinion is that if a return value is expected the function needs to throw. Otherwise the caller (function calling this function) will expect everything to be ok. Note that disp('Error') gives information to the developer, but it does not give the program any indication on what happens. More importantly, the information does not give any indication of where the error occurs. This can force the developer to do heavy debugging just to find the error, which is completely unnecessary.

The use of variable output arguments should only be used in case a different number of output arguments should be expected. An example is some customized plot function

function varargout = myplot(varargin)
filename = '';
idx = find(strcmp(varargin,'filename'));
if (~isempty(idx) && length(varargin)<idx+1 && ~ischar(varargin{idx+1}))
    error('filename property must be followed by a directory');
elseif(~isempty(idx))
    filename = varargin{idx+1};
    varargin([idx,idx+1]) = [];
end
h = plot(varargin{:});
varagout{1} = h;

if (~isempty(idx))
    save(filename, h);
end
varagout{2} = filename;

This function works as plot except it saves the figure to file in case a filename is specified. In case the developer needs the handle it will be returned and in case the developer wants the save directory it can be returned as well. None of these arguments are necessary though. The developer may want to use this function as a standard plot function and this means that the user may want to call myplot as myplot(x,y);which does not return a value. Further note that even if 'filename' is not specified, the function can still return 2 outputs. The second output may be an empty array of char, but two outputs for the caller will never cause a crash.

Also, note that no further error handling is required. The only unchecked crashes are in plot and save. How this is handled may be different for different users and this means that it only is reasonable to let the user catch the error and handle it (as he would have done if save or plot would have thrown).

Apart from this you may also want to have a check so that the number of output variables are within the correct range (in this case 0,1 or 2 outputs).

Upvotes: 0

Suever
Suever

Reputation: 65430

In your function definition, you have defined your function to allow for an unlimited number of outputs. The keyword varargout is a place-holder for a variable number of outputs.

As you have stated in your question, you only want one possible output which in your case looks to be muln. So if you simply remove varargout from your function definition, MATLAB should automatically throw an error if too many outputs are requested

function muln = my_mul(varargin)

If you ever do need to use varargout but want to place constraints on how many outputs are provided for any given scenario, you can check the number of output arguments that were requested using nargout and then throw an error with the error function.

if nargout > 4
    error('my_mul:TooManyOutputs', 'Too many outputs requested');
end

Upvotes: 1

Related Questions