getopt

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.

http://apricot.ap.polyu.edu.hk

You are now following this Submission

Usage:
opt = getopt( struct( '<opt>', <default>|'noarg' , '<opt>', <default>|'noarg', ... ), varargin{:} )
Or
[opt vararg] = getopt( struct( '<opt>', <default>|'noarg' , '<opt>', <default>|'noarg', ... ), varargin{:} )
where
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.

EXAMPLE:

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 (2026). getopt (https://se.mathworks.com/matlabcentral/fileexchange/69933-getopt), MATLAB Central File Exchange. Retrieved .

General Information

MATLAB Release Compatibility

  • Compatible with any release

Platform Compatibility

  • Windows
  • macOS
  • Linux
Version Published Release Notes Action
1.0.0