4.0

4.0 | 1 rating Rate this file 1 Download (last 30 days) File Size: 1.58 KB File ID: #25881

Process named Arguments

by Alan Robinson

 

17 Nov 2009 (Updated 20 Nov 2009)

Easily add optional, named arguments to your functions, like this: f('var_name', value).

| Watch this File

File Information
Description

Assigns named arguments passed to your function (as in plot(), etc.) to local variables. The name of the argument is the name of the local variable in your function.

 Example: inputfun('prompt', '>', 'defval', 5.1, 'timeout', 5);

Really simple and easy to use; other implementations of this idea on File Exchange require a lot of overhead to set this up, where as my code just requires one extra function call near the top of your function.

 Usage:
(1) in your function, define vars with default values;
(2) call procArgs(varargin)

 Minimal error checking: requires that all passed variable names match already defined variables in your function. It would be pretty easy to add type checking if you wanted it (I don't, particularly).

Example:

 %% example function using procargs
function test(varargin)
 name = 'John';
 age = 32;
 procArgs(varargin)
 fprintf('name=%s, age=%i\n', name, age);
 
 %% example calls
 test('name', 'Amber'); % prints Amber, 32
 test('age', 99); % prints John, 99
 test('Ages', 0); % error, will report that Ages is not defined.

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (3)
20 Nov 2009 Jan Simon

In my opinion, this will handicap the debugging drastically, because it hides the source of values.

Checking the existence of a variable with "exist('Name')" is slow and fails if a matching file, folder, class name or built-in function exists. Safer and faster: "exist('Name', 'var')". At least it is a good idea to include this test: Allow JIT compilation, avoid collisions between variables and built-in functions (can appear even for different upper/lower-case).

20 Nov 2009 Alan Robinson

Thanks Jan, I've made the change to exist that you suggest. Are you saying that this change would effect whether the JIT could compile this function or not? I would think the use of Eval would pretty much prevent that. Unless you are using a lot of named arguments inside an inner loop, however,I don't think the speed of procArgs is going to influence much. Calling it 10k times with two named arguments takes 5s on a 2.6Ghz P4 (Matlab 7.3). Nonetheless if you have suggestions on how to speed it up I'm interested in hearing them.

 I don't really see how this causes a problem with debugging though - you can always check the contents of varargin if you need to know what values were passed in.

21 Nov 2009 Jan Simon

If the EXIST test would be omitted, procArgs could even *create* variables in the caller. But this would be dangerous and would confuse the JIT compiler: "assignin('caller', 'RANK', 1)" collides with the Matlab toolbox function 'rank' (at least in Matlab 6.5 and 7.7). Therefore I appreciate that you've included (and updated) the EXIST test.
You are right: debugging is not impossible. ASSIGNIN and EVALIN can obfuscate a program and predicting the effects can be really hard. But to be honest: one can write obfuscated source even without ASSIGNIN! procArgs with some good comments can be much better than a hard coding without comments.
Rating: H1 line, help, example, version number, name of author: exsiting. Function works, is useful and usable. I personally prefer hardcoded access of variables and this might be a question of taste.

Please login to add a comment or rating.
Updates
18 Nov 2009

Improved title/description.

20 Nov 2009

Exist only checks against variable names, which is safer and faster.

Tag Activity for this File
Tag Applied By Date/Time
named arguments Alan Robinson 18 Nov 2009 10:10:13
optional arguments Alan Robinson 18 Nov 2009 10:10:13
function calls Alan Robinson 18 Nov 2009 10:10:13
utility Alan Robinson 19 Nov 2009 09:44:34
calling argument processing Alan Robinson 19 Nov 2009 09:44:35
namevalue pairs Alan Robinson 19 Nov 2009 09:44:35
parse function arguments Alan Robinson 16 Feb 2010 14:24:19

Contact us at files@mathworks.com