Set default values

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

您现在正在关注此提交

编者注: 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/

引用格式

Richie Cotton (2026). Set default values (https://ww2.mathworks.cn/matlabcentral/fileexchange/27056-set-default-values), MATLAB Central File Exchange. 检索时间: .

类别

Help CenterMATLAB Answers 中查找有关 Parallel Computing 的更多信息

一般信息

MATLAB 版本兼容性

  • 兼容任何版本

平台兼容性

  • Windows
  • macOS
  • Linux
版本 已发布 发行说明 Action
1.0.0.0