Code covered by the BSD License  

Highlights from
easyparse

4.5

4.5 | 2 ratings Rate this file 11 Downloads (last 30 days) File Size: 1.96 KB File ID: #39772

easyparse

by

 

09 Jan 2013 (Updated )

Easy-to-use interface for parsing function inputs in the form of parameter-value pairs.

| Watch this File

File Information
Description

Easy-to-use interface for converting parameter-value pairs into variables within a function. Unlike inputParser and other typical parameter-value parsers, easyparse is able to (and by default does) directly create variables within the called function, rather than as fields of a struct.

   - To create variables in the function workspace according to varargin consisting of parameter-value pairs, use this syntax in your function:
       easyparse(varargin);

   - To create only variables with allowed_names, create a cell array of allowed names and use this syntax:
       easyparse(varargin, allowed_names);

   - To create a struct with fields specified by the names in varargin, (similar to the output of inputParser) ask for an output argument:
       s = easyparse(...);

As an example, let's say you wanted a function, epdemo, that took inputs 'blee', 'bloo', and 'blah'. Then epdemo would be called with in the typical parameter value manner:
       epdemo('blee', 1:10, 'blah', 4, 'bloo', 0);

To parse these inputs within epdemo requires a single line:

       function epdemo(varargin)
           easyparse(varargin,{'blee','bloo','blah'});
           
           foo = blee*100;
           bar = bloo - blah;
           ...
       end

CAVEAT UTILITOR: this function relies on assignin statements. Input checking is performed to limit potential damage, but use at your own risk.

One of a series of simple functions to provide easier input parsing within MATLAB.

Acknowledgements

Parse Function Arguments, Parse Pv Pairs, and Parseargs: Simplifies Input Processing For Functions With Multiple Options inspired this file.

This file inspired Easydefaults.

Required Products MATLAB
MATLAB release MATLAB 7.13 (R2011b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (9)
08 Jan 2014 Jared

Hi Alan, thanks for the response and for pointing me in the direction of the procArgs function--I wasn't aware of it when I crated easyparse and easydefaults.

I think both solutions are very simple (especially since both easyparse and procArgs can take a single argument), but I might suggest that requiring procArgs to only populate existing variables in the function might be somewhat limiting.

One of the largest uses I have for easyparse--or parameter-value pairs in general--is for optional arguments: only if the caller defines a variable is that variable defined in the function. Optional arguments would seem to be difficult to achieve with procArgs. One could create artificial empty variables, but this might begin to get a little unwieldy, in my opinion.

Thanks again for the feedback!

08 Jan 2014 Alan Robinson

Much easier than Matlab's inputparser. I do something similar with my procArgs function, except I get the list of allowed variables by requiring that they already be created in the calling function, which is arguably easier still.

14 Jan 2013 Jurgen

Indeed, I was overthinking things again. easydefaults solved it, thanks :).

14 Jan 2013 Jared

Hi Jurgen,

That seems very similar to what is done by standard function inputs. For instance, if you only send three parameters to:

function demo_function(a,b,c,d,e)

then only a,b,c will be defined, with d and e ignored. So unless I am misunderstanding, in the context of parsing inputs, I'm not sure what would be gained.

Or is there another, more general application for this functionality? If there's a compelling use case, what you're describing would be pretty easy to implement based on the code in easyparse.

12 Jan 2013 Jurgen

Yes very similar to example 3 from deal(). However with deal() the number of outputs on the left must equal the number of inputs (i.e. length(C)). So if I give 10 output and only 5 input it gives an error. But I would like the first 5 to be assigned and the last 5 to be ignored.

11 Jan 2013 Jared

Hi Jurgen, glad you like it!

Can you explain your use case a bit more? As far as I understand, standard function inputs do something pretty similar to what you're describing, but perhaps I don't understand what you are looking for. Like you said, so does deal (also check Example 3 in deal, namely: [a,b,c,d] = C{:}).

Or, if perhaps you're trying to establish default arguments, you could try easydefaults, here:

mathworks.com/matlabcentral/fileexchange/39785-easydefaults

11 Jan 2013 Jurgen

Very nice, is it also possible to pass only the values for varargin, and a cell list as second argument. So that every value in varargin get sequentially mapped to a name in the list?
Or maybe deal() can do that...

10 Jan 2013 Jared

Hi Peter, I added an example function which calls easyparse to the description. Hope this helps!

10 Jan 2013 peter

can you put some examples, thanks

Updates
10 Jan 2013

Added demo to description.

10 Jan 2013

Typo fixed.

11 Jan 2013

Typo fixed.

11 Jan 2013

Typo fixed.

11 Jan 2013

Typo fixed.

15 Jan 2013

Improved input checks: now, easyparse warns you that it will not parse variables with names identical to already-defined functions to avoid unintended results.

Contact us