Process named Arguments

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

View License

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

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

This is the same idea as Matlab's inputParser, but with a focus on being really simple and easy to use. Other implementations of this idea on File Exchange require a lot of lines of code to set this up, where as my code just requires one extra function call near the top of your function.

(1) in your function, define local variables with their 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).


function test(varargin)
name = 'John';
age = 32;
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.

Cite As

Alan Robinson (2024). Process named Arguments (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2007a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Argument Definitions in Help Center and MATLAB Answers

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

Improved help text; code is identical.

better handling of different variable types.

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

Improved title/description.