Fan brother posted on 14:30:32
VC + MATLAB 7 C Shared Library
All the programs that call the shared library generated by MATLAB 7 Compiler have the following structure:
1. Declare variables or functions as input variables;
2. Call the mclInitalizeApplication function and test whether it is successful. This function sets a global MCR state and builds a MCR instance;
3. For each database, call the <libraryname> Initalize function once to create a MCR instance for the database;
4. Call the functions in the library and process the results (this is the main part of the program );
5. Call the <libraryname> Terminate function once for each database to cancel the associated MCR;
6. Call the mclTerminateApplication function to release resources associated with the global MCR status;
7. Clear the transform, close the file, and exit.
Perform exercises using the following files based on the examples provided in the help document of MATLAB:
<MATLAB root>/extern/examples/compiler/addmatrix. m
<MATLAB root>/extern/examples/compiler/multiplymatrix. m
<MATLAB root>/extern/examples/compiler/eigmatrix. m
Steps:
1) first copy these files to the current directory and then use mcc to create a shared library. The command is as follows:
Mcc-v-B csharedlib: libmatrix addmatrix. m multiplymatrix. m eigmatrix. m
The operation parameter-B csharedlib is a bound operation. Its Equivalent command is-W lib: <libname>-T link: lib.
2) create an MFC project in VC (based on the dialog box created by myself), and set the environment according to the following post:
How to Set Visual Studio to work with Matlb Complier 4.0. In this example, you only need to perform the following steps in VC:
A. Tools-> Options-> Directories-> Show directories for: Include files-> <MATLAB 7root> \ Extern \ Include;
B. Tools-> Options-> Directories-> Show directories for: Library files-> <MATLAB 7root> \ Extern \ Lib \ Win32 \ Microsoft \ msvc60;
C. Project-> Setting-> C/C ++-> Category: Code Generation-> Use run-time library: Debug Multithread DLL;
D. Project-> Setting-> Link-> Category: Input-> Object/library modules: mclmcrrt. lib libmatrix. lib (shared library generated by mcc ).
3) copy the libmatrix generated by mcc in the current MATLAB directory. h, libmatrix. dll, libmatrix. lib, and libmatrix. ctf file to the current Project directory of VC, and use Project-> Add to Project-> Files... Add libmatrix. h to the current project.
4) add # include "libmatrix. h" and # include "mclmcr. h" to the header file of the current project dialog box ";
5) initialize the MATLAB library file in BOOL cmatlab 7dlldlg: OnInitDialog () and release the MATLAB library file resources in void cmatlab 7dlldlg: OnDestroy, otherwise, the button may only be pressed once, and the second operation may fail;
6) The handler function of the function in the library file generated by MATLAB is defined in the response function of a button. Note that if an mxArray variable needs to be reused, you must use mxDestroyArray (out); out = 0; that is, you must first unregister the variable and then set it to null.
The attached functions are as follows:
1. BOOL cmatlab 7dlldlg: OnInitDialog ()
{
CDialog: OnInitDialog ();
...............
// TODO: Add extra initialization here
/* Call the mclInitializeApplication routine. Make sure that the application
* Was initialized properly by checking the return status. This initialization
* Has to be done before calling any matlab api's or MATLAB Compiler generated
* Shared library functions .*/
If (! MclInitializeApplication (NULL, 0 ))
{
AfxMessageBox ("cocould not initialize the application .");
Exit (1 );
}
/* Call the library intialization routine and make sure that
* Library was initialized properly .*/
If (! LibmatrixInitialize ())
{
AfxMessageBox ("cocould not initialize the library .");
Exit (1 );
}
Return TRUE; // return TRUE unless you set the focus to a control
}
2. void cmatlab 7dlldlg: OnDestroy ()
{
CDialog: OnDestroy ();
/* Call the library termination routine */
LibmatrixTerminate ();
MclTerminateApplication ();
}
3. void cmatlab 7dlldlg: OnRUN ()
{
CString str;
MxArray * in1, * in2;/* Define input parameters */
MxArray * out = NULL;/* and output parameters to be passed to the library functions */
Double data [] = {1, 2, 4, 5, 6, 7, 8, 9 };
/* Create the input data */
In1 = mxCreateDoubleMatrix (3, 3, mxREAL );
In2 = mxCreateDoubleMatrix (3, 3, mxREAL );
Memcpy (mxGetPr (in1), data, 9 * sizeof (double ));
Memcpy (mxGetPr (in2), data, 9 * sizeof (double ));
/* Call the library function */
MlfAddmatrix (1, & out, in1, in2 );
/* Display the return value of the library function */
Str = "The value of added matrix is: \ n ";
Str = str + Display (out );
AfxMessageBox (str );
/* Destroy the return value since this varaible will be resued in
* The next function call. Since we are going to reuse the variable,
* We have to set it to NULL. Refer to MATLAB Compiler documentation
* For more information on this .*/
MxDestroyArray (out); out = 0;
MlfMultiplymatrix (1, & out, in1, in2 );
Str = "The value of the multiplied matrix is: \ n ";
Str = str + Display (out );
AfxMessageBox (str );
MxDestroyArray (out); out = 0;
MlfEigmatrix (1, & out, in1 );
Str = "The Eigen value of the first matrix is: \ n ";
Str = str + Display (out );
AfxMessageBox (str );
MxDestroyArray (out); out = 0;
/* Free the memory created */
MxDestroyArray (in1); in1 = 0;
MxDestroyArray (in2); in2 = 0;
AfxMessageBox ("OK, Finished! ");
}
4. CString cmatlab 7dlldlg: Display (const mxArray * in)
{
CString str, strout = "";
Int I = 0, j = 0;/* loop index variables */
Int r = 0, c = 0;/* variables to store the row and column length of the matrix */
Double * data;/* variable to point to the double data stored within the mxArray */
/* Get the size of the matrix */
R = mxGetM (in );
C = mxGetN (in );
/* Get a pointer to the double data in mxArray */
Data = mxGetPr (in );
/* Loop through the data and display the same in matrix format */
For (I = 0; I <c; I ++ ){
For (j = 0; j <r; j ++ ){
Str. Format ("% 4.2f \ t", data [I * c + j]);
Strout = strout + str;
}
Strout = strout + "\ n ";
}
Strout = strout + "\ n ";
Return strout;
}
5. Attachment m file:
1) addmatrix. m
Function a = addmatrix (a1, a2)
% ADDMATRIX Add two matrices
% Copyright 2003 The MathWorks, Inc.
A = a1 + a2;
2) multiplymatrix. m
Function m = multiplymatrix (a1, a2)
% MULTIPLYMATRIX Multiplies two matrices
% Copyright 2003 The MathWorks, Inc.
M = a1 * a2;
3) eigmatrix. m
Function e = eigmatrix (a1)
% EIGMATRIX Returns the eigen value of the given matrix
% Copyright 2003 The MathWorks, Inc.
E = eig (a1 );
When publishing to the target machine, you must copy the following files: MCRInstaller.exe, executable program of the project file, shared library (DLL), and. ctf file of the shared library. Click to run the mcrinstaller.exe file. After MCR is installed, add <mcr_root> \ runtime \ win32 to the system environment variable path.