File Exchange

image thumbnail


version 1.0 (5.36 KB) by

Tool for implementing option name/value pairs in function arguments



No License

Often one desires to write a function that takes a large number of optional arguments. One way to do this is to allow for an arbitrary number of option name-value pairs in the function argument:

function r = f(a,b,c,name1,value1,...)

This method has the advantages of:

1) Allowing those using the function to specify options without needing to check the position of the option in a long list of arguments.

2) Allowing for options to have default values that need not be specified each function call.

3) Eliminates the need to deal with empty values passed for unspecified options.

loadOptions is an easy-to-use function designed to aide in writing functions taking option name/value pairs. It converts the list of name/value pairs into an easily usable options struct (a la optimset). It allows for option defaults.

And, unlike typical MATLAB syntax, the user may specify data types for each option value, or leave data types untyped, or have some values typed and others untyped.

Please see the help text / example for details on how to use loadOptions when writing a function.

loadOptions was written and tested in R2007b, but it is rather basic and should work with any release that supports varargin and class().

Comments and Ratings (1)

John D'Errico

I don't dislike this code. It has good, well written help. An H1 line. It checks for errors. The checking for class types may be useful. The code itself is good, for what it does. The problem is, its WAY too picky.

Property/value pairs (or name/value pairs if you prefer) are useful as an interface because you can be sloppy about how you type the name. You can provide the parameters in any sequence, something that fixed argument lists fail to do.

loadOptions forces you to get any capitalization absolutely correct. So as a user, you MUST type the name as 'RegisterSize', not 'registersize'. Caps are important in this code. Also, suppose you find yourself feeling lazy? Can you just type the name as 'reg'? No. This code will tell you its not a valid option. It does not try to figure out that 'reg' is the first three letters of the valid name 'RegisterSize' in the example provided. As such, it is an unambiguous shortening of that name. A good code will do that for you. Note that the property/value pairs interfaces in Matlab do exactly that. Look at the handle graphics tools, or at optimset or odeset. Or, look at my own parse_pv_pairs here on the file exchange.

Making this code work in a friendly way to be case insensitive or to handle unambiguous shortenings is trivial, only a couple of additional lines. Look into the functions lower and strmatch, also the strcmp family of functions.

If the author were to repair these problems, I would happily revise my review.

MATLAB Release
MATLAB 7.5 (R2007b)

Inspired: parseParameters

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

» Watch video