Created
March 23, 2012 09:23
-
-
Save chris-taylor/2168885 to your computer and use it in GitHub Desktop.
Set default values for Matlab function arguments
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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