Set default values
Updated 23 Mar 2010
In order to set default values for variables, I find the start of my functions littered with
if nargin < 1 || isempty(x)
x = 1;
if nargin < 2 || isempty(y)
y = 3;
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
Richie Cotton (2023). Set default values (https://www.mathworks.com/matlabcentral/fileexchange/27056-set-default-values), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform CompatibilityWindows macOS Linux
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!
Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.