How does matlab implement function overload?

Source: Internet
Author: User

How does matlab implement function overload? Below I will use a simple output input number of the maximum number of functions fun () to explain how Matlab in the editing function to implement overload.

Method/Step:

1. First open matlab (I am using the Matlab 2015a, the use of other versions of the MATLAB has little impact). In the Common window, enter edit fun (fun can be changed to any character, only for demonstration purposes), open the M file editor, the M file has been created, the file name is fun.

2. In the editor input function Y=fun (varargin), Varargin is the MATLAB internal variable, the type is a cell array, if changed to other variables will be an error.

3. Add code narginchk (2,3); Guarantee the number of inputs, when the number is less than 2 or greater than 3 o'clock program will error.

4.Matlab There is a nargin variable used to store the number of inputs in the function. The following uses the IF statement to implement the overload of the function.

Input

If nargin==2

A=varargin{1};

B=VARARGIN{2};

Y=max ([a,b]);

...

With this code, a function can be used to enter two of a quantity.

Where a is used to store the first amount of input, and B to hold the second quantity of the input quantity. The max () function returns the largest element of the input matrix.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.