[英]Various input for one function in matlab
I know that I can have various input number by using varargin
. 我知道可以使用varargin
获得各种输入数字。
However, if you type sparse(
in MATLAB, you can see that the input argument names are different for the number of input arguments. 但是,如果在MATLAB中键入sparse(
,则可以看到输入参数名称因输入参数的数量而不同。
I want to make such input style. 我想做这样的输入样式。
Is it possible to make such function? 可以实现这种功能吗? or is it only limitted to built-in function? 还是仅限于内置功能?
You can do this using the inputParser
class and the addOptional
method. 您可以使用inputParser
类和addOptional
方法执行此操作。 Basically, MATLAB checks for the argument and if it does not exist in the function call, assigns a default value to it. 基本上,MATLAB会检查参数,如果函数调用中不存在该参数,则为其分配默认值。
addOptional(p,argName,default) adds optional input, argName, to the input parser scheme of inputParser object, p. addOptional(p,argName,default)将可选输入argName添加到inputParser对象p的输入解析器方案。 When the inputs that you are checking do not include a value for this optional input, the input parser assigns the default value to the input. 当您要检查的输入不包含此可选输入的值时,输入解析器会将默认值分配给该输入。
addOptional(p,argName,default,validationFcn) specifies a validation function for the input argument. addOptional(p,argName,default,validationFcn)指定输入参数的验证函数。
See docs for inputParser and addOptional for examples. 请参阅docs中的inputParser和addOptional的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.