Code covered by the BSD License  

Highlights from
Default arguments

image thumbnail

Default arguments

by

 

25 Apr 2013 (Updated )

Set default values for optional function input.

defaultargs(defaults,varsin)
function varargout = defaultargs(defaults,varsin)
% use to create default values for optional function inputs
% example:
% function examplefunc(input1,input2,varargin)
% default_options = {1, false, 'good', [1,2,3]};
% [opt1,opt2,opt3,opt4] = defaultargs(default_options,varargin);
% % function code

varargout = defaults;
for n = 1:length(defaults)
        if ~(length(varsin) < n || isempty(varsin{n}))
            varargout(n) = varsin(n);
        end
end
if length(varsin) > length(defaults); warning('defaultargs:Extrainput','Last %d input(s) ignored',length(varsin) - length(defaults)); end

Contact us