在MATLAB中具有灵活的有序/无序和带标签/无标签输入列表的函数 [英] Functions with a flexible list of ordered/unordered and labeled/unlabeled inputs in MATLAB

查看:92
本文介绍了在MATLAB中具有灵活的有序/无序和带标签/无标签输入列表的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

许多MATLAB函数具有如下输入结构:

A lot of MATLAB functions have an input structure such as:

output = function MyFun(a,b,c,'-setting1',s1,'-setting2',s2,'-setting3',s3)

我想知道如何在自己的函数中实现这种功能.确切地说,我想了解如何创建这样的函数:

I am wondering how I should implement this kind of functionality in my own functions. To be precise, I would like to find out how I can create a function such that:

  1. 该函数具有可变数量的输入N + M

第一个N输入是有序且未标记的.在上面的示例中,N = 3.第一个输入始终为a,第二个输入始终为b,第三个输入始终为c.函数输入是可变的,因为用户不必发送bc.如果不这样做,则它们可以采用默认(硬编码)值.据我所知,这种功能通常是通过varargin.

The first N inputs are ordered and unlabeled. In the example above, N = 3. The first input is always a, second input is always b, third input is always c. The function input is variable in that users do not necessarily need to send b, c; when they do not then these can take on default (hardcoded) values. As far as I know, this type of functionality is generally handled via varargin.

其余的M输入是无序的,但带有标签.在上面的示例M = 3中,变量分别为s1,s2,s3,它们的标签分别为setting1setting2setting3,我希望用户能够以任意顺序指定这些变量想.如果用户选择不指定这些输入之一(即setting1),那么我希望函数为s1.

The remaining M inputs are unordered, but labeled. In the example above, M = 3, the variables are s1,s2,s3 and their labels are setting1,setting2 and setting3 respectively, I would like for users to be able to specify these variables in whatever order they want. If users choose not to specify one of these inputs (i.e. setting1), then I would like my function to assign default values for s1.

此类功能的一个示例是 dlmwrite 功能.

One example of such a function is the dlmwrite function.

理想情况下,我正在寻找MATLAB开发人员通常使用的方法,以便使我的代码易于理解.

Ideally, I am looking for an approach that is typically used by MATLAB developers so that my code is easy to understand.

推荐答案

InputParser 类解决了所有这些问题.您可以指定任意数量:

The InputParser class addresses all of these issues. You can specify any number of:

  1. 必需的参数(有序,无标签)
  2. 可选参数(有序,无标签)
  3. 以任意顺序(无序,标记)的字符串参数/值对


MathWorks提供了非常清晰的带示例的教程.对于定义为function printPhoto(filename,varargin)的函数,该示例可归结为以下内容.


A very clear tutorial with examples is provided by MathWorks. For a function defined as function printPhoto(filename,varargin), the example boils down to the following.

创建inputParser:

p = inputParser;

指定默认值并定义验证条件:

Specify defaults and define validation criteria:

defaultFinish = 'glossy';
validFinishes = {'glossy','matte'};
checkFinish = @(x) any(validatestring(x,validFinishes));

defaultColor = 'RGB';
validColors = {'RGB','CMYK'};
checkColor = @(x) any(validatestring(x,validColors));

defaultWidth = 6;
defaultHeight = 4;

定义必需/可选/参数输入名称,设置其默认值和验证功能:

Define required/optional/parameter input names, set their default values and validation functions:

addRequired(p,'filename',@ischar);
addOptional(p,'finish',defaultFinish,checkFinish);
addOptional(p,'color',defaultColor,checkColor);
addParameter(p,'width',defaultWidth,@isnumeric);
addParameter(p,'height',defaultHeight,@isnumeric);

将输入解析为结构:

parse(p,filename,varargin{:});

然后您在p.Results中输入了输入参数及其值.

Then you have the input arguments and their values in p.Results.

InputParser类在新的MathWorks函数中使用,因此不要害怕自己使用它!

The InputParser class is used throughout newer MathWorks functions, so don't be afraid to use it yourself!

这篇关于在MATLAB中具有灵活的有序/无序和带标签/无标签输入列表的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆