Optional function arguments

버전 3.1.1.0 (79.2 KB) 작성자: Bill Whiten
Optional function arguments with default values: optndfts
다운로드 수: 734
업데이트 날짜: 2018/9/26

라이선스 보기

Optional function arguments: optndfts
Allows optional arguments to be specified as name value pairs, and
default values given for those augments not specified e.g.
function strt=demo1(arg1,varargin) % insert values to replace defaults
strt=optndfts(varargin,'aa',1,'bb',2,'cc',3); % default values
return % optndfts returns a struct
end
demo1(123,'bb',22,'aa',11)
ans =
aa: 11
bb: 22
cc: 3
Result is a struct, varagin input values can be list of name value pairs,
cell array of name value pairs, or a struct.
Also can allow for optional arguments that are not named:
function strt=demo2(arg1,varargin) % insert values to replace defaults
strt=optndfts(varargin,{'aa','bb'},'aa',1,'bb',2,'cc',3,'dd',4);
return % optndfts returns a struct
end
demo2(123,11,22,'dd',44)
ans =
aa: 11
bb: 22
cc: 3
dd: 44
demo2(123,11,'dd',44)
ans =
aa: 11
bb: 2
cc: 3
dd: 44
demo2(123,'dd',44)
ans =
aa: 1
bb: 2
cc: 3
dd: 44
A second output argument if present is a struct containing name value
pairs not given in the default values, these can be used to override
defaults for functions called within original function.
Functions selectfields and renamefields, with error messages suppressed,
can also be used to assist in passing options from an outer function
to nested functions (see doc selectfield, and doc renamefields).
Test files contain examples.
See doc optndfts

인용 양식

Bill Whiten (2024). Optional function arguments (https://www.mathworks.com/matlabcentral/fileexchange/38881-optional-function-arguments), MATLAB Central File Exchange. 검색됨 .

MATLAB 릴리스 호환 정보
개발 환경: R2015a
R2016b 이상 릴리스와 호환
플랫폼 호환성
Windows macOS Linux
카테고리
Help CenterMATLAB Answers에서 Dialog Boxes에 대해 자세히 알아보기

Community Treasure Hunt

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

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

Includes reference to rename fields and selectfields

3.1.0.0

Now can use char or string

3.0.0.0

One example adjust to give output.
V3.0 is compatible with previous versions. Adds additional functionality for passing options to functions called within another function
Reloaded

2.0.0.0

v3.0 (2015-09-07) compatible with v2.0 and v1.0
Improved handling of cell arrays in optional arguments
Compatible with v1.0
Now includes optional initial unnamed arguments
Now includes second output for other name values pairs
v2.0 compatible with v1.0
Now includes optional initial unnamed arguments
Now includes second output for other name values pairs

1.0.0.0