parse_pv_pairs

版本 1.0.0.0 (2.1 KB) 作者: John D'Errico
Parsing property/value pairs for function input.
4.7K 次下载
更新时间 2006/1/16

查看许可证

An interface that uses property/value pairs is a
good way to provide sets of parameters which have
defaults, but where there may be many possible
parameter options. The user does not want to
remember the sequence of possibly dozens of
arguments, many of which may remain at their
default values.

First, set default values for the parameters.
Assume we have four parameters that we wish to
use optionally in the function examplefun.

- 'viscosity', which will have a default value of 1
- 'volume', which will default to 1
- 'pie' - which will have default value
3.141592653589793
- 'description' - a text field, left empty by default

The first argument to examplefun is one which will always be supplied in this example.

function examplefun(dummyarg1,varargin)
params.Viscosity = 1;
params.Volume = 1;
params.Pie = 3.141592653589793
params.Description = '';
params=parse_pv_pairs(params,varargin);
params

Use examplefun, overriding the defaults for 'pie',
'viscosity' and 'description'. The 'volume' parameter
is left at its default value.

examplefun(rand(10),'pie',3,'vis',10, ...
'Description','Hello world')

params =
Viscosity: 10
Volume: 1
Pie: 3
Description: 'Hello world'

Note that capitalization was ignored, and the
property 'viscosity' was truncated to 'vis' as
supplied. Also note that the order the pairs were
supplied was arbitrary.

Warning: parse_pv_pairs does not check for data
type in any value, nor for validity of the value
should there be any constraints. These are features
I'd like to add in the future.

引用格式

John D'Errico (2024). parse_pv_pairs (https://www.mathworks.com/matlabcentral/fileexchange/9082-parse_pv_pairs), MATLAB Central File Exchange. 检索来源 .

MATLAB 版本兼容性
创建方式 R14SP1
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Argument Definitions 的更多信息
标签 添加标签

Community Treasure Hunt

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

Start Hunting!
版本 已发布 发行说明
1.0.0.0

Mlint suggested some changes.