问题描述
怎么可以声明函数在MATLAB与可选ARG的?
how can i declare function in matlab with optional arg's?
例如:函数[A] =列车(X,Y,OPT)
,其中选择必须是一个可选的参数
for example: function [a] = train(x, y, opt)
, where opt must be an optional argument.
推荐答案
有关于如何做这几个不同的选择。最基本的是使用 ,然后用 nargin
,尺寸
等来判断是否可选参数已经传递给函数。
There are a few different options on how to do this. The most basic is to use varargin
, and then use nargin
, size
etc. to determine whether the optional arguments have been passed to the function.
一个多一点优雅的寻找解决方法是使用类来定义你的函数所期望的所有参数,都必需和可选。 inputParser
,您还可以进行类型检查,对所有的参数。
A little more elegant looking solution is to use the inputParser
class to define all the arguments expected by your function, both required and optional. inputParser
also lets you perform type checking on all arguments.
这篇关于在MATLAB功能可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!