File Exchange

image thumbnail

Default arguments

version 1.6.0.0 (1.24 KB) by Bas-Jan
Set default values for optional function input.

1 Download

Updated 26 Apr 2013

View Version History

View License

Set default arguments for use in function, without having to code the switch-case blocks. Code is quite trivial, but it saves me some time coding.

Cite As

Bas-Jan (2021). Default arguments (https://www.mathworks.com/matlabcentral/fileexchange/41497-default-arguments), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (1)

Jan

What do you expect as result of this line:
varargin = varargin{:};
I assume, it should be deleted to avoid errors.
The code can be simplified:
varargout = defaults;
n = min(numel(defaults), numel(varargin));
varargout(1:n) = varargin(1:n);
if nargin > n
% error instead of a warning
end
An error is much more reliable than a warning, which will be overseen when a mutli-million line program fils the command window with status messages.

MATLAB Release Compatibility
Created with R2013a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!