View License

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

» Watch video

Highlights from
parseargs: Simplifies input processing for functions with multiple options

5.0 | 2 ratings Rate this file 22 Downloads (last 30 days) File Size: 5.83 KB File ID: #10670 Version:

parseargs: Simplifies input processing for functions with multiple options


Malcolm Wood (view profile)


07 Apr 2006 (Updated )

For parsing function arguments supplied as name-value pairs, e.g. 'XLim',[0 10],'YLim',[0 100]

| Watch this File

File Information

Based on same idea as the excellent "parse_pv_pairs", and similar to the "inputParser" object added to MATLAB in R2007a, this function handles names and values for properties in any order, and assigns the values to a structure containing default values.
For example, in your function which has options "StartTime" and
"StopOnError", create a structure of defaults and pass inputs to parseargs:

| function myfunction(varargin)
| X.StartTime = 0;
| X.StopOnError = true;
| X = parseargs(X,varargin{:})

If the user specifies inputs:
| myfunction('StopOnError',false,'StartTime',5)
the structure will be modified accordingly.

The aim of this function is to ease as much as possible the tedious and error prone task of handling inputs, specifically with a view to application development (rather than quick scripting), where robustness is of high importance.

To this end, the function enforces some rules, partly to reduce the chance of mistakes by the caller, and partly to save the user from writing additional code to enforce them:
1) Property names must be specified in full, and are case-sensitive
2) The data type of a property cannot be changed (unless the property was initially empty)
3) Scalar values must remain scalar
4) Where the original value is a cell array it must contain strings. The new value must be one of these strings, and if no new value is specified, the first string is selected as a default.


Parse Function Arguments and Parse Pv Pairs inspired this file.

This file inspired Variogramfit, Experimental (Semi ) Variogram, Continuous Sound And Vibration Analysis, Input Preprocessor, and Easyparse.

MATLAB release MATLAB 6.1 (R12.1)
Other requirements Tested with MATLABs 6.1 through 7.10
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (5)
09 Mar 2012 batman52

I am trying this, but it's not passing the argcheck of inputParser...

function [varargout] = struct2varargin(instruct)

fields = fieldnames (instruct);
values = struct2cell (instruct);

varargout = {};
% length(fields)
% length(values)

for idx =1:length(values)
varargout{2*idx-1} = char(fields{idx});
varargout{2*idx} = values{idx};


Comment only
09 Mar 2012 batman52

Does it exist somewhere, the inverse function for this?

What I need to do is:

savestate = f_getstate();

Input parameters for f_setstate has to be pv pairs, because i am using inputParser to parse it.
At the same time, I like savestate being a structure, because it's easier to hanlde than pv pairs.

Comment only
13 Aug 2008 Dimitri Shvorob

Obsolete: 'help inputparser' for Matlab 2007 and later.

Comment only
10 Apr 2006 Yogesh Mehta

Thanks. Just what I wanted.

08 Apr 2006 John D'Errico

Its a nice variant that you may find useful. This uses a more strongly typed style than parse_pv_pairs. Parseargs can check for errors in input, but it is more restrictive. It will not allow you to have a property that is by default a numeric variable, but may also be a character string.
Good help, well done.

17 Dec 2009 1.1


30 Apr 2010 1.2

Added copyright lines and suppressed M-Lint warnings.

01 Sep 2016

Updated license

Contact us