File Exchange

image thumbnail


version 1.0.0 (2.26 KB) by Chi-Hang Lam
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 08 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 (2019). getopt (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (0)

MATLAB Release Compatibility
Created with R2015b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Discover Live Editor

Create scripts with code, output, and formatted text in a single executable document.

Learn About Live Editor