Matlab call function with optional arguments

Matlab call function with optional arguments

Posted: OLGA_84 Date of post: 07.07.2017

There are a few different options on how to do this. The most basic is to use varargin , and then use nargin , size etc. 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.

A simple way of doing this is via nargin N arguments in. The downside is you have to make sure that your argument list and the nargin checks match. It is worth remembering that all inputs are optional, but the functions will exit with an error if it calls a variable which is not set.

The following example sets defaults for b and c.

matlab call function with optional arguments

Will exit if a is not present. A good way of going about this is not to use nargin , but to check whether the variables have been set using exist 'opt', 'var'.

math - Optional args in MATLAB functions - Stack Overflow

See this answer for pros of doing it this way: How to check whether an argument is supplied in function call? By posting your answer, you agree to the privacy policy and terms of service.

Stack Overflow Questions Developer Jobs Documentation beta Tags Users. Sign up or log in to customize your list. Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us. Log In Sign Up. Join the Stack Overflow Community.

Stack Overflow is a community of 7.

Join them; it only takes a minute: Optional args in MATLAB functions Ask Question. How can I declare function in MATLAB with optional arguments? Nayuki 13k 5 28 Yekver 1, 4 23 Good discussions of this here blogs.

Evgeni Sergeev 7, 8 50 Sign up or log in StackExchange. Sign up using Facebook.

Optional input arguments on Matlab function - Stack Overflow

Sign up using Email and Password. Post as a guest Name.

Stack Overflow works best with JavaScript enabled. MathOverflow Mathematics Cross Validated stats Theoretical Computer Science Physics Chemistry Biology Computer Science Philosophy more 3.

matlab call function with optional arguments

Meta Stack Exchange Stack Apps Area 51 Stack Overflow Talent.

Rating 4,3 stars - 917 reviews
inserted by FC2 system