Optional args in MATLAB functions
问题 How can I declare function in MATLAB with optional arguments? For example: function [a] = train(x, y, opt) , where opt must be an optional argument. 回答1: 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. % Function that takes two arguments, X & Y, followed by a variable % number of additional arguments function varlist(X,Y,varargin) fprintf('Total