File Exchange

image thumbnail

Parse function arguments

version 1.0 (5.45 KB) by

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

32 Downloads

Updated

No License

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)

Comments and Ratings (11)

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'}});
===============================
I am gettin error on my machine with matlab version R2014a.... as below..
Undefined function 'parseArgs' for input arguments of type cell.
Args=parseArgs(Args,'Holdaxis',{'Spacing' {'sh','sv'}; 'Padding' {'pl','pr','pt','pb'}; 'Margin' {'ml','mr','mt','mb'}},varargin{:});

Can any one help me to solve ASAP..

ABID

ABID (view profile)

If we want to use single ylable instead of two or three, how can we do this???

Amir

Amir (view profile)

Excellent, nice & elegant. Exactly what I needed!

JB Vincent

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

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

Patrik Eschle

Saved me a day of headache, thanks!

Ulrik Ulrik

very nice, just what I needed

Philippe Arrchambault

Exactly what I was looking for. Thanks!

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.

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)

MATLAB Release
MATLAB 6.0 (R12)

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

» Watch video