Proficient in combined VC and Matlab programming (6)
Author: Deng Ke
Download source code
Analysis of combined VC and Matlab programming <1>
Analysis on combined programming of VC and Matlab <2>
Analysis of combined VC and Matlab programming <3>
Analysis of combined VC and Matlab programming <4>
Analysis of combined VC and Matlab programming <5>
The matlab c/C ++ function library is an important part of the MATLAB Extension function. It contains a large number of MATLAB functions re-compiled in C/C ++ language, it mainly includes elementary mathematical functions, linear algebra functions, matrix operation functions, numerical calculation functions, special mathematical functions, polynomial functions, and interpolation functions, you can call these functions in your own C/C ++ program to implement complex matrix computing functions, thus reducing the workload of programming, note that the C function library and C ++ function library are two different function libraries.
Before use, install:
In the MATLAB command window, enter:
Mbuild-Setup
Follow the prompts to select the VC compiler. After completion, enter the following in the MATLAB command window:
Mex-Setup
Select the VC compiler to complete the installation and configuration of the compiler.
First, we will introduce the C function library. The library functions required by the C mathematical function library are in this directory: MATLAB root directory:/extern/lib
The following is an example of using the C function library provided by MATLAB (all examples of using C functions are: MATLAB root directory/extern/examples/cmath ):
* Ex1.c ** copyright (c) 1995-1999 by the Mathworks, Inc. all rights reserved. * // * $ revision: 1.6 $ */# include <stdio. h> # include <stdlib. h>/* used for exit_success */# include <string. h> # include "Matlab. H "/* Matlab. H is the header file of the C mathematical function library static double real_data [] = {1, 2, 3, 4, 5, 6}; static double cplx_data [] = {7, 8, 9, 10, 11, 12}; int main () {/* declares two mxarray matrix variables and assigns the initial value null */mxarray * mat0 = NULL; mxarray * mat1 = NULL; /* use automatic memory management */mlfenternewcontext (0, 0);/* Create a matrix mat0, mat1 and assign values */mlfassign (& mat0, mlfdoublematrix (2, 3, real_data, null);/* This matrix is a 2*3 matrix mlfassign (& mat1, mlfdoublematrix (3, 2, real_data, cplx_data);/* This matrix is a 2*3 matrix, and it is a complex matrix/* print matrix */mlfprintmatrix (mat0); mlfprintmatrix (mat1);/* release the memory occupied by the matrix */mxdestroyarray (mat0); mxdestroyarray (mat1 ); /* Disable Automatic Memory Management */mlfrestorepreviouscontext (0, 0); Return (exit_success );}
Image Description at the bottom of the image display:
Figure 1 program running result
The procedure is as follows:
1. Open the ex1.c file in VC (the file is located in this directory: MATLAB root directory/extern/examples/cmath)
2. Set the path of the header file and library file: tool-> select, directory
Add the following path to the inlude option:
D:/Matlab/extern/include/CPP
D:/Matlab/extern/include
Result 2:
Figure 2 header file path settings
Add the following path to the library option: 3 D:/Matlab/extern/lib/Win32/Microsoft/msvc60 D:/Matlab/extern/lib D:/Matlab/bin.
Figure 3 File Path settings
3. Add the library file: Project-> Settings-> link. In the general option, Set Directory D in "Object Library/module: write the file names of all libraries in the/Matlab/extern/lib/Win32/Microsoft/msvc60 Directory: 4
Figure 4 Add a library file
After all the settings are complete, you can run them. The MATLAB built-in function library includes the most basic mathematical functions in matlab c mathematical functions. Any application written using the C mathematical function library must use the MATLAB built-in function. The matlab m file function library contains functions converted from M files. These functions are richer than built-in functions, and contain a large number of special functions in the matlab c function library, the MATLAB array is declared as an mxarray struct. The struct is defined in the directory Matlab/extern/include/matrix. in the H file, a good understanding of the mxarray struct helps to use the matlab c math library.
Common functions of matlab c built-in math include: common functions; operators and special matrices; basic matrix operation functions elementary mathematical functions linear algebra functions data analysis and Fourier transformation functions string functions file input and output functions common data types functions matlab m file function library operations and special functions basic matrices of functions operational Functions basic mathematical functions special mathematical functions linear algebra functions Numerical Analysis and Fourier transformation functions polynomials and interpolation functions and Constant Differential Equations string functions Files Read and Write Functions data types functions Sparse Matrix Functions
The usage of the above functions is described in the help document of MATLAB. Similar to the matlab c ++ function library, the matlab c ++ function library also includes the matlab c ++ built-in mathematical Library and the matlab m file function library, in addition, many functions are encapsulated based on the matlab c function library, so that the C ++ language can call C ++ library functions by using functions such as overload and inheritance. The difference between the matlab c ++ function library and the matlab c function library is that the matlab c ++ function library uses classes to define data structures, while the matlab c function library uses struct to define data structures. Note: The C/C ++ function library is no longer used in the MATLAB 7.0 version.