C language implementation MatLab 6.5 Method

xiaoxiao2021-03-06  18

Abstract: This article focuses on the use of the MATLAB command to translate the M file into a C language program, and modify the method of using the C language function that can be directly called, the use of the method to pay attention to the method of the method and the difference in MATLAB version. Using this method will enable C language to directly use mathematical calculation functions that have been completed in Matlab, greatly expand the mathematical calculation function and development efficiency of C language.

Key words: matlab; m file; C language interface

As we all know, MATLAB is a powerful mathematical software, which is good at completing various mathematical functions in a matrix operation. However, its program needs to be interpreted in a matLab environment and is not efficient. If you can use the powerful library for C language, use C to compile execution, MATLAB will be able to play a greater role. Therefore, MATLAB has provided an application interface with external C / C programs from 5.0, providing possibilities for functions that use C language to call MATLAB. However, MATLAB's interface has developed very quickly, and MATLAB 6.5 has provided support for VC 7.0, and a certain change in the interface of C relative to 5.x.

In Matlab, we use M files to implement functions, each M file implements a separate feature, and this function in this and C language corresponds to each other. So, if we can translate the M files in matlab into a single function in the C language, you can implement the corresponding features in MATLAB.

Implementation

The entire process can be divided into three main parts. Use MATLAB to translate M files into C language files, extract useful statements from the generated C language file, and write data conversion programs to implement parameter format conversion. The entire process will eventually translate M files into a function of the same function as a C language for other programs.

This article uses a simplest M file:

File Name: MersSay.m

Function C = Messay () a = 3.4; b = 5.6; C = SQRT (a) SQRT (b);

The m file is implemented

1. Compile M file as a C language file

In order to translate M files into C language files, a certain setting is required, here it is assumed that the environment in which the C language is written is VC6.0, enter the MEX -SETUP and MBUILD -SETUP commands at the MATLAB command prompt, and select Microsoft in the appropriate option. Visual C / C can be.

Use the MCC command to translate Messay.m into C code in the MATLAB command line.

MCC -M MersSay.m

The parameters of the parameters - M represent the MCC command will translate the M file into the C language code.

The translation command will generate three C language files under the folder where Messay.m is located: MESSAY.H, MESSAY.C and MESSAY_MAIN.C. Where Messay_Main.c provides main () function; Messay.h provides a function declaration of the entire program; Messay.c contains the function function generated by MATLAB. Among these three files, Messay.c contains the mathematical functions we need.

2. Extract useful statement

Through analysis, it is found that the internal parameter transfer method generated by the MCC is specified by the MATLAB link library, which is difficult to change, so useful code needs to be extracted, and change the parameter transfer method of generating code. At the same time, from the annotation of the generated code, the code segment that really contains the M file function is all in the MMESSAY () function (the default constructor of the function name is the prefix M file name), while others Generate functions only implement the functionality of parameter delivery and standardization interface services.

Therefore, the specific method of extracting code is to use the Static MxArray * MMessay (int Nargout_) function generated in Messay.c, and other generated functions can be ignored. Original generated Mmessay () code is as follows: static mxArray * Mmessay (int nargout_) {mexLocalFunctionTable save_local_function_table = mclSetCurrentLocalFunctionTable (& _ local_function_table_messay); mxArray * c = NULL; mxArray * b = NULL; mxArray * a = NULL; mlfAssign (& a, _mxarray0_) MLFASSIGN (& B, _MxArray1_); MLFASSIGN (MlfsQRT (Mlfsqrt (MLFSQRT (MLFSQRT (MLFSQRT (MCLVV (B, "B")))); MCLVALIDATEOUTPUT (C, 1, Nargout_, "C "," messay "); MXDESTROYARRAY (A); MXDESTROYARRAY (B); MCLSETCURRENTLOCALFUNCTIONTABLE (Save_Local_Function_Table_); return c;

Among the generated code, the MCLSetCurrentLocalFunctionTable and MclsetCurrentLocalFunctionTable functions are two external functions, pass the parameters to the outside, and the relevant parties have no effect on the C program. There is only one sentence that is actually useful and executed:

Mlfassign (& C, MclPlus (mlfsqrt (MLFSQRT (MCLVV (A, "A")), MLFSQRT (MCLVV (B, "B")))))))))))))

In fact, in the C statement translated by Matlab, most of the statements related to the statement and the generated function are starting with MLF, so the simple way to find a useful statement is to directly find the code of MLF prefix.

3, parameter format conversion

It should be noted that all of MATLAB is based on a data structure called MxArray, all floating point numbers, vectors, or matrices are stored and transmitted through the MxaRay structure. Of course, all mathematical functions provided by MATLAB are also calculated based on such a data structure. Therefore, to use MATLAB generation code, you must convert the floating point numbers and integers commonly used in the C language to the MxArray structure.

This example utilizes the MATLAB function MxArray * mlfscalar (double V) and function Double * mxgetpr (mxaRay *) to implement parameter format conversion. Function mlfscalar () stores Double type variables into a new MxaRray structure and returns a pointer, and the function MxGetPr () takes out the real part of the real number saved by the MxaRay structure. As for other parameter conversion methods, refer to the related portions in Reference 3.

Finally, you can write such a function that utilizes the MATLAB math function and implements the calculation:

Double MMessay (Double INA, Double Inb) {mxArray * a, * b, * c; // Three parameters Double * OUTC; // calculate the result variable A = mlfscalar (Double) INA) ; // Use mlfscalar () to convert B = mlfscalar ((Double) INB); MLFASIGN (& C, MclPlus (mlfsqrt (MLFSQRT (MLFSQRT (MLFSQRT (MCLVV (B, "B"))) ); OUTC = MxGETPR (C); // C Getting the results of the results, that is, the results MXDESTROYARRAY (A); // Release space MXDESTROYARRAY (B); MXDESTROYARRAY (C); return * outc;} to this, the entire translation process Completed, but it can't be called directly. Mathematical library functions MLFSQRT (), mlcplus () and data conversion functions mlfscalar () and MxgetPR () are used in this function. Since these functions are cured in the link library, in order to connect, several library files and several static link library lib files must be added. The required library files are the library files added in the MCC command generated, generally libmatlb.h, and the static link library file that needs to be added is as follows:

Libmat.lib, libmatlb.lib, libmex.lib, libmx.lib

If there is no above file, you can convert the corresponding DEF file corresponding to the VC to lib files, and the transformation format is lib /def:filename.def / machine: IX86 /out:filename.lib. Limitation of the method

Using the methods used herein can translate M files into C language functions, but are subject to two factors.

1, the implementation of the function is limited by the MATLAB C function library

The mechanism for this translation is provided by MATLAB, and the MCC command can directly translate only to the original function of MATLAB. Because these functions have been compiled by Matlab6.5, generally in mlf prefix, exist in dynamic link libraries and can be called directly by the C language. These functions can be found in Reference 4. More than this range of functions, and in the m files are nested, when translating with MCC, the MCC will add prefix MLF before the function name and further translate the function.

However, this translation is limited by the MATLAB parameter, and cannot be called directly, and the error can not be found when compiling the corresponding external function. The solution is to manually adjust all of the translated functions. If the function included in the m file is deeply labeled in the nest, such a workload is huge and unacceptable. At the same time, many functions defined in the toolbox also cannot be translated with this method. MATLAB's toolbox update speed is very fast, and the corresponding MATLAB C function libraries have a hysteresis, causing a lot of functions in the latest toolbox that they cannot be translated.

2. Translation of the existence of itself

Because this translation is complied with C language requirements, therefore the memory allocation requirements and C language different functions and some functions of the graphic display type (including a large number of GUI related functions) cannot be correctly translated. For example, Mash.m and Step.m These two more common MATLAB functions, due to the above limitations, they cannot be translated by this method.

For the above problems, the MATLAB library function can be dynamically called using the method of using the MATLAB engine in the C program, basically solving all of the above functions cannot be correctly translated and graphic display. However, the method of using the MATLAB engine requires the use of ActiveX automation servers. When running, the program performs a MATLAB thread in the background without completely detaching the MATLAB environment, which means that in pure C is unable to apply, Matlab must be installed first and can support multi-threaded work at runtime. Specific methods can refer to Reference 3. Differences between Matlab 5.x and Matlab 6.5

There is a difference between the following points, you need to pay attention to this method:

(1) The reservoir required in the program is changed from the 5.x version of Matrix.h, MCC.H, Matlab.h to MEX.H, LIBMATLB.H, Libmatlbm.h and other library files.

(2) The static link library file required to join is changed from 5.x to libmmfile.lib, libmatlb.lib, libmcc.lib, libmax.lib, libmat.lib, libmatlb.lib, libmex.lib, libmx.lib Four files.

(3) API function changes, although the mathematical library is the function of the MLF prefix changes, but the function of variable establishment, memory management, and data type conversion changes, that is, many original MCC prefixes are changed to prefix with MX. The function is replaced, making many 5.x translated C program codes fail to run through 6.5 corresponding libraries.

(4) The 6.5 version directly adds support for M files in the VC environment. After executing the MBUILD -SETUP configuration command, Matlab provides Matlab Project Wizard in the VC to create MATLAB projects directly to translate M files in the VC environment. However, this translation method translates the wrong function name into a variable name when the undefined function is nested, and the MCC command will further translate the internal nested functions.

转载请注明原文地址:https://www.9cbs.com/read-45961.html

New Post(0)