Code covered by the BSD License  

Highlights from
Set default values

5.0

5.0 | 1 rating Rate this file 13 Downloads (last 30 days) File Size: 1.32 KB File ID: #27056

Set default values

by Richie Cotton

 

23 Mar 2010

A very simple function that initialises a missing or empty value in the caller function.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information
Description

In order to set default values for variables, I find the start of my functions littered with

if nargin < 1 || isempty(x)
   x = 1;
end

if nargin < 2 || isempty(y)
   y = 3;
end
etc.

This is pretty ugly, so I've created a wrapper to prettify it. Honestly, it's so simple that I nearly didn't upload this, but it does make your functions cleaner. Now the above is transformed to

SetDefaultValue(1, 'x', 1);
SetDefaultValue(2, 'y', 3);

Note that there are other ways to set defaults. See
http://blogs.mathworks.com/loren/2009/05/05/nice-way-to-set-function-defaults/

MATLAB release MATLAB 7.9 (2009b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
23 Mar 2010 David Young  
Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
default Richie Cotton 23 Mar 2010 11:00:38
inputs Richie Cotton 23 Mar 2010 11:00:38
utility Richie Cotton 23 Mar 2010 11:00:38
potw Shari Freedman 02 Apr 2010 09:55:50
pick of the week Jiro Doke 11 Feb 2011 20:07:31

Contact us at files@mathworks.com