View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Default arguments

Be the first to rate this file! 2 Downloads (last 30 days) File Size: 1.24 KB File ID: #41497 Version: 1.6
image thumbnail

Default arguments



25 Apr 2013 (Updated )

Set default values for optional function input.

| Watch this File

File Information

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.

MATLAB release MATLAB 8.1 (R2013a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (1)
26 Apr 2013 Jan Simon

Jan Simon (view profile)

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
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.

Comment only
26 Apr 2013 1.4

optimized code

26 Apr 2013 1.6

Optimized code

Contact us