Application programming interfaces of Matlab and C Language Programs

Source: Internet
Author: User
The Application Programming Interface of Matlab and C language programs-general Linux technology-Linux programming and kernel information. The following is a detailed description. MATLAB, as the world's top mathematical application software, uses its powerful engineering computing, algorithm research, engineering drawing, application development, data analysis, dynamic simulation, and other functions, it plays an increasingly important role in aerospace, mechanical manufacturing, engineering and construction. C language features are rich, flexible and convenient to use, and the target program efficiency is high. It has both the advantages of advanced languages and the characteristics of low-level languages. Therefore, C language is currently the most widely used programming language. Although MATLAB is a complete and functional programming environment, in some cases, the interaction with external environment data and programs is very necessary and beneficial.

As we all know, MATLAB is programmed in M Language and cannot directly call C language programs in M files. You can use the application programming interface (API) provided by MATLAB to implement external interfaces, in the MATLAB environment, you can call C or Fortran programs, input or output data, and establish customer/Server relationships with other software programs.

The C language program called in MATLAB must be implemented through the MEX file.

I. Structure of the MEX file in C Language

A c-language MEX file is a dynamic connection subroutine that can be called just like an M file. MEX files are mainly used in the following aspects:

(1) In MATLAB, the calculation speed of M files, especially the cyclic iteration speed, is far slower than that of C language. Therefore, you can use C language to compile the portion requiring a large number of cyclic iterations as the MEX file, increase the computing speed.
(2) If a C language program has been developed, it does not have to be converted into an M file and repeat the work. By adding the Entry Program, the mexFunction can be called by MATLAB.
(3) direct control of hardware, such as A/D Acquisition Card and D/A output card, for data collection or control applications.

The C-language MEX file source program consists of two very obvious parts:
(1) The computing program, that is, the program code that completes the computing function in the MEX file. The computing program can be a common C language program and can be compiled according to the C language rules.
(2) The entry program connects the computing program to the entry function of the MATLAB, namely, the mexFunction. The Entry Program Mexico function is relatively complex. The function has four parameters: nlhs, plhs, nrhs, and prhs. Here, nlhs is the number of output data, plhs is the pointer to the output data of mxArray (all data in MATLAB is defined by mxArray), nrhs is the number of input data, prhs is a pointer to the input data of mxArray.

Ii. Use the MEX File

The two main parts of the MEX file described above can be used independently or in combination. In either case, the MEX file must contain the header file "mex. h" to properly declare the portal program. The entry program name must be mexFunction and contain these parameters:

Void mexFunction (int nlhs, mxArray * plhs [], int nrhs, const mxArray * prhs [])

In the MEX file of C language, the parameters nlhs and nrhs contain the number of output and input variables. With these two parameters, the MEX file is called. The plhs and prhs parameters are vectors that contain pointers to the output and input variables of the MEX file. prhs is a pointer array of input variables whose length is nrhs, plhs is a pointer array of output variables whose length is nlhs. For example, you can call a MEX file from the MATLAB command window:
X = fun (y, z );

Then, the MATLAB compiler uses the following variables to call the mexFunction:
Nlhs = 1
Nrhs = 2
Plhs = (pointer)->/* unassigned */
Prhs = (pointer)-> y
(Pointer)-> z

Plhs points to a C language array with only one element, and this element is a null pointer. Prhs points to the C language array containing two elements. The first element points to mxArray variable Y, and the second element points to mxArray variable Z.

Here, plhs points to an empty array because the output x has not been generated before the subprogram is executed. The portal program is used to create an output array and assign the pointer plhs [0] to the array. If plhs [0] is not assigned a value, MATLAB will give a warning message that the output variable is not assigned a value.

Iii. Example of C language program MEX File

The MATLAB 5 API provides a series of programs to process various data types supported by MATLAB. Each data type has a corresponding function that you use to process the corresponding data. The following shows a simple C Language Program and the corresponding MEX file code. The function of this program is to double the scalar x.

The C language program is as follows:


# Include "math. h"
Void timestwo (double y [], double x [])
{
Y [0] = 2.0 * x [0];
Return;
}

The following files share the same functions as the C language program:
# Include "mex. h"
Void timestwo (double y [], double x [])
{
Y [0] = 2.0 * x [0];
}
Void mexFunction (int nlhs, mxArray * plhs [], int nrhs, const mxArray * prhs [])
{
Double * x, * y;
Int mrows, ncols;
/* Check the correct number of parameters */
If (nrhs! = 1 ){
MexErrMsgTxt ("an input parameter is required .");
}
Else if (nlhs> 1 ){
MexErrMsgTxt ("too many output parameters .");
}
/* The input variable must be a non-plural scalar */
Mrows = mxGetM (prhs [0]);
Ncols = mxGetN (prhs [0]);
If (! MxIsDouble (prhs [0]) | mxIsComplex (prhs [0]) |! (Mrows = 1 & ncols = 1 )){
MexErrMsgTxt ("the input variable must be a non-plural scalar .");
}
/* Create a matrix for the returned parameters */
Plhs [0] = mxCreateDoubleMatrix (mrows, ncols, mxREAL );
/* Allocate pointers to input and output parameters */
X = mxGetPr (prhs [0]);
Y = mxGetPr (plhs [0]);
/* Call the timestwo subfunction */
Timestwo (y, x );
}

C checks function parameters during compilation. MATLAB can transmit any number and type of parameters in the M function, as is the case in the MEX file, but the number of input and output parameters must be reliably processed in the program. If you name the above MEX file timestwo. c, you can compile and link it. In the MATLAB command window, enter:
Mex timestwo. c

This is a required step to generate the MEX file timestwo. ***. the type of the system platform where the file extension runs. In Windows, the extension is dll. In this case, you can call timestwo just like calling the M function.
In the MATLAB command window, enter:
X = 2;
Y = timestwo (x );
You can get:
Y = 4;

Iv. Small knot

Although the MEX file has powerful functions, it is not appropriate for all applications. MATLAB is an efficient programming system, especially suitable for engineering computing, System Simulation and other applications. Its biggest advantage is to free people from complicated programs. Therefore, programs that can be completed using M files should be written in MATLAB whenever possible, unless the MEX files must be used.

The Application Programming Interface of MATLAB is a powerful system, in addition to calling C or Fortran programs, it can also input and output data from the MATLAB environment, and establish customer/Server relationships with other programs in the MATLAB environment.
Related Article

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.