View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Option parser

Be the first to rate this file! 5 Downloads (last 30 days) File Size: 3.09 KB File ID: #40383 Version: 1.0

Option parser



A generalized option parser for Toolbox functions

| Watch this File

File Information

Supports keyword options that have a value, are a boolean flag, or enumeration types (string or int).

The software pattern is:
  function(a, b, c, varargin) = false; = true;
      opt.blah = [];
      opt.choose = {'this', 'that', 'other'}; = {'#no', '#yes'};
      opt = optparse(opt, varargin);

Optional arguments to the function behave as follows:
  'foo' sets <- true
  'nobar' sets <- false
  'blah', 3 sets opt.blah <- 3
  'blah', {x,y} sets opt.blah <- {x,y}
  'that' sets opt.choose <- 'that'
  'yes' sets <- 2 (the second element)

and can be given in any combination.

If neither of 'this', 'that' or 'other' are specified then opt.choose <- 'this'.

Alternatively if:
       opt.choose = {[], 'this', 'that', 'other'};
then if neither of 'this', 'that' or 'other' are specified then opt.choose <- []

If neither of 'no' or 'yes' are specified then <- 1.

- That the enumerator names must be distinct from the field names.
- That only one value can be assigned to a field, if multiple values are required they must be converted to a cell array.
- To match an option that starts with a digit, prefix it with 'd_', so the field 'd_3d' matches the option '3d'.
The allowable options are specified by the names of the fields in the structure opt. By default if an option is given that is not a field of opt an error is declared.

Sometimes it is useful to collect the unassigned options and this can be achieved using a second output argument
          [opt,arglist] = tb_optparse(opt, varargin);
which is a cell array of all unassigned arguments in the order given in varargin.

Required Products MATLAB
MATLAB release MATLAB 8.0 (R2012b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (1)
30 Jun 2013 Muhammad

Hi.. Mr Peter
What is the function of Option parser?
Can you give the example that we have to use this function and the example that we don't need to use this function?

Comment only

Contact us