No BSD License  

4.42857

4.4 | 7 ratings Rate this file 44 Downloads (last 30 days) File Size: 5.45 KB File ID: #3448

Parse function arguments

by

 

16 May 2003 (Updated )

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 file inspired Parseargs: Simplifies Input Processing For Functions With Multiple Options and Easyparse.

MATLAB release MATLAB 6.0 (R12)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (8)
25 Oct 2012 Amir

Excellent, nice & elegant. Exactly what I needed!

27 Mar 2007 JB Vincent

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

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).

30 Aug 2006 Patrik Eschle

Saved me a day of headache, thanks!

17 Aug 2006 Ulrik Ulrik

very nice, just what I needed

27 Jan 2005 Philippe Arrchambault

Exactly what I was looking for. Thanks!

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.

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)

Contact us