Default arguments

バージョン 1.0.0.0 (2.64 KB) 作成者: Ged Ridgway
Set missing/empty variable or field to given value
ダウンロード: 472
更新 2009/12/15

ライセンスの表示

This simple helper-function lets you replace commonly seen input-argument handling code with shorter and clearer alternatives. For example, instead of:
if nargin < 2
x = 42;
end
if ~isfield(options, 'verbose')
options.verbose = false;
end
if ~isfield(options, 'method')
options.method = 1;
end

you can simply have:
default('x', 42)
default(options, 'verbose', false)
default(options, 'method', 1)

By avoiding nargin, you can more easily change function definitions without messing up all of the argument numbers, e.g. changing: function blah(input, verbose)
to: function blah(method, input, options, verbose)
only requires new default commands for method and options arguments, whereas use of nargin would need to change for input and verbose.

I find this useful -- I hope others do too! :-)

引用

Ged Ridgway (2025). Default arguments (https://www.mathworks.com/matlabcentral/fileexchange/26111-default-arguments), MATLAB Central File Exchange. に取得済み.

MATLAB リリースの互換性
作成: R14SP3
すべてのリリースと互換性あり
プラットフォームの互換性
Windows macOS Linux
カテゴリ
Help Center および MATLAB AnswersEntering Commands についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
バージョン 公開済み リリース ノート
1.0.0.0