Set default values
Nota del editor: This file was selected as MATLAB Central Pick of the Week
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/
Citar como
Richie Cotton (2024). Set default values (https://www.mathworks.com/matlabcentral/fileexchange/27056-set-default-values), MATLAB Central File Exchange. Recuperado .
Compatibilidad con la versión de MATLAB
Compatibilidad con las plataformas
Windows macOS LinuxCategorías
Etiquetas
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Descubra Live Editor
Cree scripts con código, salida y texto formateado en un documento ejecutable.
Versión | Publicado | Notas de la versión | |
---|---|---|---|
1.0.0.0 |