File Exchange

image thumbnail

Optional function arguments

version (79.2 KB) by Bill Whiten
Optional function arguments with default values: optndfts


Updated 26 Sep 2018

View Version History

View License

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
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
return % optndfts returns a struct
ans =
aa: 11
bb: 22
cc: 3
dd: 44
ans =
aa: 11
bb: 2
cc: 3
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

Cite As

Bill Whiten (2021). Optional function arguments (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)

Bill Whiten

Test functions are prototype applications not complete functions, and contain warnings regarding missing components. The functions can all be run to see possible applications of the optndfts function.

Bill Whiten

Stephen Cobeldick: Code in first example in doc optndfts adjusted, semicolon removed as in file, thanks.
Other examples should be working ok.
The results of running the examples are in the files.
return is optional but not invalid, I regard it as good practice as return end clearly marks the end of code.

Stephen Cobeldick

@Bill Whiten: the examples you give in the function itself are not valid MATLAB code. For example:

% function optntest2(arg1,varargin) % varargin replaces defaults
% optn=optndfts(varargin,{'bb','aa'},'aa',1,'bb',2,'cc',3)
% return % optndfts returns a struct
% end

Does not return anything as you do not specify an output argument, so the description "optndfts returns a struct" is incorrect. Also the RETURN is totally superfluous: although it does not cause any error MATLAB does not require RETURN at the end of functions, and none of the examples in the MATLAB documentation show RETURN for that purpose:

You should pay careful attention to the mlint messages and editor warnings to fix basic mistakes like these ones, and always test your code before using it as an example and uploading it.

MATLAB Release Compatibility
Created with R2015a
Compatible with R2016b and later releases
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!