Code covered by the BSD License  

Highlights from
parse_pv_pairs

5.0

5.0 | 5 ratings Rate this file 16 Downloads (last 30 days) File Size: 2.09 KB File ID: #9082

parse_pv_pairs

by

 

18 Nov 2005 (Updated )

Parsing property/value pairs for function input.

| Watch this File

File Information
Description

An interface that uses property/value pairs is a
good way to provide sets of parameters which have
defaults, but where there may be many possible
parameter options. The user does not want to
remember the sequence of possibly dozens of
arguments, many of which may remain at their
default values.

First, set default values for the parameters.
Assume we have four parameters that we wish to
use optionally in the function examplefun.

 - 'viscosity', which will have a default value of 1
 - 'volume', which will default to 1
 - 'pie' - which will have default value
     3.141592653589793
 - 'description' - a text field, left empty by default

The first argument to examplefun is one which will always be supplied in this example.

  function examplefun(dummyarg1,varargin)
  params.Viscosity = 1;
  params.Volume = 1;
  params.Pie = 3.141592653589793
  params.Description = '';
  params=parse_pv_pairs(params,varargin);
  params

Use examplefun, overriding the defaults for 'pie',
'viscosity' and 'description'. The 'volume' parameter
is left at its default value.

   examplefun(rand(10),'pie',3,'vis',10, ...
     'Description','Hello world')

params =
     Viscosity: 10
       Volume: 1
              Pie: 3
 Description: 'Hello world'

Note that capitalization was ignored, and the
property 'viscosity' was truncated to 'vis' as
supplied. Also note that the order the pairs were
supplied was arbitrary.

Warning: parse_pv_pairs does not check for data
type in any value, nor for validity of the value
should there be any constraints. These are features
I'd like to add in the future.

Acknowledgements

This file inspired Argcheck, Filefun: Apply A Function To Files, Parse Property/Value Pairs And Structures, Input Preprocessor, Parseargs: Simplifies Input Processing For Functions With Multiple Options, and Easyparse.

MATLAB release MATLAB 7.0.1 (R14SP1)
Other requirements Any release of matlab which can use structures should be able to use parse_pv_pairs.
Tags for This File   Please login to tag files.

No tags are associated with this file.

Please login to add a comment or rating.
Comments and Ratings (5)
04 Feb 2013 IainIsm  
08 Sep 2010 Richard Crozier

Very useful

22 Jan 2010 Joshua Arnott

Brilliantly elegant.

03 Jul 2008 Tom Clark

Excellent little utility - I'm now using this in at least 50% of the functions I write!

Cheers John.

29 Dec 2005 David Epstein

Easy to use and well-documented file. This file does exactly what I wanted, and using it has saved me a lot of time.

The program does include some error-checking, but, as the author points out, some additional error checking might be an advantage, in particular a check that the type of a user-provided override matches that of the default. Unfortunately I don't know enough to do this myself (and if the default is a uint32, it may be OK if the override is a double, an annoying complication to such an extension of error-checking).

Contact us