Code covered by the BSD License  

Highlights from
Optional function arguments

Be the first to rate this file! 10 Downloads (last 30 days) File Size: 7.68 KB File ID: #38881 Version: 2.0
image thumbnail

Optional function arguments

by

 

02 Nov 2012 (Updated )

Optional function arguments with default values: optndfts

| Watch this File

File Information
Description

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

See doc optndfts

Acknowledgements

This file inspired Linfitregsel Term Selection In Linear Regression, Greyboxbuild: Complete A Greybox Model, and Greyboxeval Model Quality Evaluation.

Required Products MATLAB
MATLAB release MATLAB 8.5 (R2015a)
MATLAB Search Path
/
/__MACOSX
/__MACOSX/optndfts
/__MACOSX/optndfts/tests
/optndfts
/optndfts/tests
Other requirements Will run in many most Matlab versions.
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Updates
29 Jul 2015 2.0

Compatible with v1.0
Now includes optional initial unnamed arguments
Now includes second output for other name values pairs

29 Jul 2015 2.0

v2.0 compatible with v1.0
Now includes optional initial unnamed arguments
Now includes second output for other name values pairs

Contact us