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.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
There are a few different options on how to do this. The most basic is to use
varargin, and then usenargin,sizeetc. to determine whether the optional arguments have been passed to the function.A little more elegant looking solution is to use the
inputParserclass to define all the arguments expected by your function, both required and optional.inputParseralso lets you perform type checking on all arguments.