MATLAB and C Language Program application programming interface

Source: Internet
Author: User

As the world's top mathematical application software, MATLAB plays an increasingly important role in the fields of aerospace, mechanical manufacture and engineering construction, with its powerful engineering calculation, algorithm research, engineering drawing, application development, data analysis and dynamic simulation. and C language is rich in function, flexible and convenient to use, high efficiency of target program. Both the advantages of advanced language, but also the characteristics of low-level language. Therefore, the C language is currently the most widely used programming language. Although Matlab is a complete, full-featured programming environment, in some cases, the interaction with the external environment data and programs is very necessary and beneficial.

As we all know, Matlab is programmed with M language, can not directly call the C language program in M file. MATLAB can provide the application programming interface (API) to achieve interface with the external, in the MATLAB environment to achieve the call C language or FORTRAN program, input or output data, and other software programs to establish customer/server relations and other functions.

Matlab to call the C language program, must be implemented through the Mex file.

The structure of MEX files in C language

The C language Mex file is a dynamic connection subroutine that can be invoked like an M file. Mex files are mainly used in the following aspects:

(1) in MATLAB, m file calculation speed, especially the cyclic iteration speed is much slower than the C language, so you can request a large number of cyclic iterations in C language to write as Mex file, improve the calculation speed.

(2) has developed the C language program, you do not have to convert it to M files and duplication of labor, by adding the entry program Mexfunction, can be called by MATLAB.

(3) Direct control hardware, such as A/D acquisition card, D/A output card, etc. for data acquisition or control applications.

The source program for Mex files in C is made up of two very obvious parts:

(1) Calculation program, that is, in the Mex file to complete the calculation function of the program code, the calculation program can be ordinary C language program, in accordance with the C language rules can be written

(2) The entrance program, the calculation program and MATLAB connected to the entry function mexfunction. The entry program Mexfunction is relatively complex, with four parameters nlhs, PLHS, NRHS, and PRHS in the function. Here NLHS is the number of output data, PLHS is pointing to the Mxarray (MATLAB, all the data are defined by the Mxarray) of the output data pointer, NRHS is the number of input data, PRHS is a pointer to the input data of the Mxarray.

Figure 1 shows how to enter data into the Mex file, through the interface function to call the calculation program to complete the operation process, and finally to the MATLAB to return the results of the process.

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.