Skip to content

Instantly share code, notes, and snippets.

@chris-taylor
Created March 23, 2012 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chris-taylor/2168885 to your computer and use it in GitHub Desktop.
Save chris-taylor/2168885 to your computer and use it in GitHub Desktop.
Set default values for Matlab function arguments
function SetDefault(argname,value)
%SETDEFAULT Provide a default value of VALUE for the argument ARGNAME.
%
%USAGE
% SetDefault('x',1)
% If no variable 'x' exists in the calling function, or if the variable exists
% but it is empty, then this call assigns the value 1 to 'x'. If the variable
% 'x' already exists, and is non-empty, then no action is taken.
%
% Author: Chris Taylor
% Created: 20/02/2012
existstr = ['~exist(''' argname ''')'];
emptystr = ['isempty(' argname ')'];
if evalin('caller',existstr) || evalin('caller',emptystr)
assignin('caller',argname,value)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment