在MATLAB功能可选参数 [英] Optional Arguments in matlab functions

查看:162
本文介绍了在MATLAB功能可选参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

怎么可以声明函数在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.

推荐答案

有关于如何做这几个不同的选择。最基本的是使用 varargin ,然后用 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 类来定义你的函数所期望的所有参数,都必需和可选。 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功能可选参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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