No BSD License  

Highlights from
Parse function arguments

4.33333

4.3 | 6 ratings Rate this file 17 Downloads (last 30 days) File Size: 5.45 KB File ID: #3448

Parse function arguments

by Aslak Grinsted

 

16 May 2003 (Updated 27 Oct 2004)

A useful tool for parsing function arguments like 'cellspacing',6,'monkey',1

| Watch this File

File Information
Description

Helper function for parsing varargin. Makes it easy to write functions that take arguments like this: subaxis(4,2,1,'spacing',0,'marginleft',.1,'H','pt',.1)
 
 
  ArgStruct=parseArgs(varargin,ArgStruct[,FlagtypeParams[,Aliases]])
 
  * ArgStruct is the structure full of named arguments with default values.
  * Flagtype params is params that don't require a value. (the value will be set to 1 if it is present)
  * Aliases can be used to map one argument-name to several argstruct fields
 
 
  example usage:
  --------------
  function parseargtest(varargin)
 
  %define the acceptable named arguments and assign default values
  Args=struct('Holdaxis',0, ...
         'SpacingVertical',0.05,'SpacingHorizontal',0.05, ...
         'PaddingLeft',0,'PaddingRight',0,'PaddingTop',0,'PaddingBottom',0, ...
         'MarginLeft',.1,'MarginRight',.1,'MarginTop',.1,'MarginBottom',.1, ...
         'rows',[],'cols',[]);
 
  %The capital letters define abrreviations.
  % Eg. parseargtest('spacingvertical',0) is equivalent to parseargtest('sv',0)
 
  Args=parseArgs(varargin,Args, ... % fill the arg-struct with values entered by the user
            {'Holdaxis'}, ... %this argument has no value (flag-type)
            {'Spacing' {'sh','sv'}; 'Padding' {'pl','pr','pt','pb'}; 'Margin' {'ml','mr','mt','mb'}});
 
  disp(Args)

Acknowledgements
This submission has inspired the following:
parseargs: Simplifies input processing for functions with multiple options
MATLAB release MATLAB 6.0 (R12)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (7)
24 Aug 2004 Michael Kadour

Problem with flagparams - fixed with minor modification to line 69:

Fnames{i,4}=isempty(strmatch(Fnames{i,1},FlagTypeParams)); %Does this parameter have a value? (e.g. not flagtype)

27 Oct 2004 Aslak Grinsted

Thanks. However, there is a problem with the above fix. It will cause you to specify a value for flagparams. Flagparams are meant to be default false, but then true if you set it. F.x. You could call:
subaxis(1,'hold')

which would set argstruct.hold=true... In the newest version it is optional if you want to specify a value for flag params. e.g. you could call:
subaxis(1,'hold',0)
to set hold to false.

27 Jan 2005 Philippe Arrchambault

Exactly what I was looking for. Thanks!

17 Aug 2006 Ulrik Ulrik

very nice, just what I needed

30 Aug 2006 Patrik Eschle

Saved me a day of headache, thanks!

26 Nov 2006 Chris BL

Nice job. Thanks!
I was immediately stopped by a bug:
If I want some parameters to be able to receive an empty value, ie [], your script fails. (It concludes that the parameter is numeric or something).

27 Mar 2007 JB Vincent

Good but it seems impossible to use more than one flag-type argument (an argument with no value).

Please login to add a comment or rating.
Updates
27 Oct 2004

Improved flag parameter functionality.

Tag Activity for this File
Tag Applied By Date/Time
parse Aslak Grinsted 22 Oct 2008 07:01:39
function Aslak Grinsted 22 Oct 2008 07:01:39
string Aslak Grinsted 22 Oct 2008 07:01:39
arguments Aslak Grinsted 22 Oct 2008 07:01:39
inputparser Aslak Grinsted 01 Jul 2011 06:45:14

Contact us at files@mathworks.com