As we all know, Matlab is a powerful mathematical software, good at using matrix operations to complete a variety of mathematical functions. But the program needs to be interpreted and implemented in the MATLAB environment, the efficiency is not high. If you can use its powerful library for C language, use C to compile and execute, MATLAB will be able to play a greater role. So, Matlab has already provided the application interface with the external C + + program from 5.0, which provides the possibility for using the function of the C language to invoke Matlab. But the interface of MATLAB development is very fast, to MATLAB 6.5 already provided to VC 7.0 support, at the same time to C interface relative to 5.X version has a certain change.
In the MATLAB, we use the M file to implement functions, each of the M file to achieve a separate function, this point and C language functions are mutually corresponding. So, if we can transform the M file in MATLAB into a single function under C language, we can realize the corresponding function in MATLAB.
Implementation methods
The whole process can be divided into three main parts, using MATLAB to translate m file into C language file, extract useful statements from the generated C language file, and write data conversion program to realize parameter format conversion. The whole process will eventually translate the M file into a function of the same function in C language for other programs to call.
This article uses one of the simplest m files to sample:
FileName: messay.m
function c=messay()
a=3.4;
b=5.6;
c=sqrt(a)+sqrt(b);
The M file implements the calculation
1, the M file compiled into C language files
In order to translate m files into C language files, it is necessary to set up a certain setting, where the C-language environment is written as VC6.0, at the MATLAB command prompt, enter mex-setup and Mbuild-setup commands, select Microsoft Visual C + + in the corresponding options + + can be.
In the MATLAB command line, the MCC command is used to translate MESSAY.M into C code.
mcc -m messay.m
The parameter-m represents the MCC command, which translates the M file into C language code.
The translation command will generate three C language files in the same folder as MESSAY.M: Messay.h,messay.c and MESSAY_MAIN.C. The MESSAY_MAIN.C provides the main () function, Messay.h provides the function declaration of the whole program, and Messay.c contains the function function of MATLAB. Of these three files, messay.c contains the mathematical functions we need.