Set default values

버전 1.0.0.0 (1.32 KB) 작성자: Richie Cotton
A very simple function that initialises a missing or empty value in the caller function.
다운로드 수: 1.2K
업데이트 날짜: 2010/3/23

라이선스 보기

편집자 메모: 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 (2024). Set default values (https://www.mathworks.com/matlabcentral/fileexchange/27056-set-default-values), MATLAB Central File Exchange. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R2009b
모든 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Parallel Computing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!
버전 게시됨 릴리스 정보
1.0.0.0