To be used in functions for parsing varargin (the list of input arguments of a variable length) and extracting a set of arguments and flags.
Updated 8 Jan 2019

View License

opt = getopt( struct( '<opt>', <default>|'noarg' , '<opt>', <default>|'noarg', ... ), varargin{:} )
[opt vararg] = getopt( struct( '<opt>', <default>|'noarg' , '<opt>', <default>|'noarg', ... ), varargin{:} )
varargin = {'<opt>', [<value>], '<opt>', [<value>], ... }

A pair '<opt>', <default> specifies an argument:
If '<opt>',<value> is found in varargin, opt.<opt> = <value>
Otherwise, opt.<opt> = <default>.

A pair '<opt>', 'noarg' specifies a flag:
If '<opt>' is found in varargin, opt.<opt> = 1.
Otherwise, opt.<opt> = 0.

Options and flags in varargin can appear in arbitrary order.

vararg returns a list of unmatched entries in varargin,
which is useful as arguments for a further level of function call.


varargin = {'length',10, 'cube', 'density',13}; % typically assigned via a function call instead
opt = getopt(struct('length',0, 'width',0, 'height',0, 'density',1, 'cube','noarg' ), varargin{:});
It returns:
opt =
length: 10
width: 0
height: 0
density: 13
cube: 1

Cite As

Chi-Hang Lam (2024). getopt (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2015b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Argument Definitions in Help Center and MATLAB Answers

Community Treasure Hunt

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

Start Hunting!
Version Published Release Notes