Use MATCOM to implement Matlab-based hybrid programming

Source: Internet
Author: User
Use MATCOM to implement Matlab-based hybrid programming
Source: Neural Networks Author: AI club comments 0 views 42

MATCOM is the earliest compiler from MATLAB to C ++ launched by mathtools. Later, because mathtools was acquired by MATLAB, mtacom has not released a new version for Versions later than MATLAB 5.3. Compared with the compiler provided by MATLAB, converting code with MATCOM is much simpler and more convenient. For projects and researchers using C ++, MATCOM's c ++ matrix Library (matrix Lib) there is still a great value for use.
There are three methods to connect to a scripting language or advanced algorithm language such as M functions through MATCOM:-compile m Files into C ++ code and insert the code into the C ++ project, finally, it is compiled into an independent executable program. The second is to directly use the matrix <lib> provided by MATCOM in the C ++ program. The method is similar to one but is commonly used. The last one is to use mideva to directly generate the EXE file and then use shell to call it in VC. The method is simple, but the versatility is poor. It is only applicable to the use of MATLAB in VC to achieve graphic display.
The mideva software platform provides nearly a thousand basic functions of MATLAB. Through necessary settings, you can directly implement mixed programming with C ++, instead of relying on Matlab, mediva also provides the compilation and Conversion Function to convert MATLAB functions or compiled MATLAB programs into C ++ DLL, so as to effectively call MATLAB functions and processes from the MATLAB environment, it is possible to realize the use of MATLAB powerful toolbox functions.
The disadvantage of mideva is that the application software compiled by C ++ and Matlab must carry the necessary DLL, which increases the volume of the software (about 4 MB ), at the same time, it cannot provide support for all MATLAB functions, such as some functions designed using class libraries.
This section takes VC ++ 6.0 and MATCOM as examples. 1. describes in detail how to use MATCOM for joint programming of VC ++ and Matlab (I .e., 1); 2, this section describes how to use the matcom c ++ matrix Library (II) in VC ++ ).
1. Currently, MATCOM has a maximum version of 4.5. First install MATCOM 4.5, as shown in figure

 

 

 

:

 

 

 

Install VC ++ 6.0 before installing MATCOM. The Select compiler dialog box appears during installation. Select "yes", for example:

If you choose whether or not Matlab is installed, you can select "Yes (y)" for confirmation if MATLAB 5.3 is installed on the local machine, because matcom4.5 does not support Matlab 6.1 or later versions. Other options are set by default. MATCOM can run independently of Matlab, but requires an external C ++ compiler. It is used to specify the location of MATLAB to locate some system functions required in the compilation file.
After the installation is complete, under "<MATCOM root directory> lib", you can find the header file matlib that uses the matcomc ++ matrix Library (matrix Lib. H and v1_1v. lib file. Under the System32 directory of the Windows operating system directory, you can find the dynamic link library file v1_1v using the matcomc ++ matrix library. DLL. Start the mideva interface, such. The mideva integrated development environment includes the command line window, variable List window, command List window, and compilation link information window. Detailed Help documents are provided.
 

 

The MATCOM command input method is the same as that of Matlab. If the system is installed in the Chinese version, enter a space before the command. Otherwise, garbled characters are displayed.

The following describes in detail how to compile m Files and put them in VC ++.
Start mideva, file-> new, and create the following huatu. M file.
The default location for saving m Files is MATCOM installation directory matcom45. The default location for saving the generated C ++ files is MATCOM installation directory matcom45debug. In this example, the installation directory of MATCOM is drive C: matcom45debug, which will generate huatu in this directory. h. huatu. CPP, huatu. r, huatu. OBJ file.

Use MFC wizard (exe) in VC ++ to create a project named example based on the dialog box. Add an idc_button1 button on the panel.

C: copy the contents of the huatu. cpp file under the matcom45debug directory to void cexamledlg: onbutton1 () as follows,
Void cexampledoc: onbutton1 ()
{
DMM (x); DMM (y );

# Line 1 "C:/matcom45/huatu. m"
Call_stack_begin;
# Line 1 "C:/matcom45/huatu. m"
_ X = colon (-2.0, 0.1, 2.0 );
# Line 2 "C:/matcom45/huatu. m"
_ Y = power (x, 2.0 );
# Line 3 "C:/matcom45/huatu. m"
_ Display (plot (CL (x), y )));
Call_stack_end
}

Set matlib under C: matcom45lib. H and v1_1v files and C: The huatu1.h files under matcom45debug are copied to the example directory of the project, and then the library files and header files are added to the project in VC: project-> Add project-> files, and select matlib from the directory just copied to example. h. v1_1v and huatu1.h files
5. Add the following code to examledlg. cpp:
# Include "matlib. H"
# Include "huatu. H"
In void cexamledlg: onbutton1 (), add an initialization class library to call the function "initm (matcom_version)" and an end class library to call the function "exitm ()" as follows.
Void cexampledoc: onbutton1 ()
{
Initm (matcom_version)
DMM (x); DMM (y );

# Line 1 "C:/matcom45/huatu. m"
Call_stack_begin;
# Line 1 "C:/matcom45/huatu. m"
_ X = colon (-2.0, 0.1, 2.0 );
# Line 2 "C:/matcom45/huatu. m"
_ Y = power (x, 2.0 );
# Line 3 "C:/matcom45/huatu. m"
_ Display (plot (CL (x), y )));
Call_stack_end
Exitm ()
}
Run the program and click the Drawing button:
 

The initialization and release of the matcom c ++ matrix library appear in pairs. during initialization, call the initm (matcom_version) function and call the function exitm () when it needs to be released. Matcom c ++ matrix library initialization takes some time. To speed up program execution, we should minimize the number of matcom c ++ matrix library initialization times. Therefore, developers should carefully consider the length of the life cycle of the matcom c ++ matrix library, and initialize the C ++ Library at the beginning of the life cycle of the matcom c ++ matrix library, release the C ++ Library at the end of the lifecycle. This will occupy a certain amount of memory, but will increase the speed of running the program.
MATCOM can not only generate C ++ files, but also generate EXE files and DLL files. :
 

2. The following example shows how to use the matcom c ++ matrix library in Visual C ++ 6.0:
In Visual C ++ 6.0, create a Win32 console project, such. In fact, matrix <lib> can be used in any type of C ++ project. For the sake of simplicity, the console program is used as an example.
 

2. Set the project and open the project | setting link tag. For the following modifications, see:

Open the C/C ++ label and make the following changes:
 

Add the header file matlib. h to check whether it is successful? Failed to check settings!
Enter the following program in example. cpp:
# Include "stdafx. H"
# Include "stdio. H"
# Include "matlib. H"
Int main (INT argc, char * argv [])
{
Initm (matcom_version );
Mm;
A = rand (3, 3 );
Display ();
Exitm ();
Return 0;
}
Output result:
Ans (3x3) = 9 double elements real (72 bytes) =
0.9501 0.4860 0.4565
0.2311 0.8913 0.0185
0.6068 0.7621 0.8214
This explains that the matcom c ++ matrix library adopts an object-oriented design style, and the matrix data and operations are encapsulated in class mm. When creating a matrix, you only need to declare a mm object instance. For example, mm;

The COM component generated by MATLAB is described as follows:
1. For any Matlab function used to generate COM, It is a com method in the COM component, while the global variable in the Matlab function is an attribute of the COM component. This is very useful for neural network programming. For example, when we perform simulation after Ann training, we need to call the COM component generated by MATLAB. However, obviously, we cannot read Ann configurations (such as weights, offsets, and other item values) every time we call COM, nor can we see MATLAB structs or classes in VC. In this case, we can create a Matlab function that is used to call the ANN configuration file (generally the. Mat file ). If you declare Ann's configuration quantity as a global variable, you can directly use this variable in the simulation function. As shown in the following example:
Function Y = sim_ann (X)
Global Net
Y = SIM (net, X );

Function loadannparam
Global Net
The % annmat. Mat file stores the net variable.
Load annmat net

If the COM object is set to comobj when called in VC, the following call method is available:
Comobj. loadannparam (); // load the ANN Parameter
For (INT I = 0; I <n; I ++)
Comobj. sim_ann (1, x, y); // simulate Ann multiple times

2. You can customize the Data Type in MATLAB, that is, the class in VC. However, when a COM component is generated, Matlab does not recognize this type when loading the object of the new class. In this case, we can initialize an object of the same name before loading the class. For example, if we have a new class like newclass, We need to load an object of this type named newobj, and the object's initialization function is initnewclass, then we can:
Newobj = initnewclass;
Load mat_file newobj;

3. In addition, this problem has been encountered during programming. If the output variable of the COM interface is a struct, sometimes the peak may sound inexplicably. This problem cannot be found by multiple parties. If you encounter a similar situation during use, you can consider the solution.

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.