COM (2) for VC and Matlab Interfaces)

Source: Internet
Author: User

First, edit the m function file in the MATLAB Editor: Start Matlab-> file-> New-> M-file function content 1: This function has no input/output parameters, and the file is saved as huatu. m.
Function huatu ()
X =;
Y = sin (X );
Plot (x, y );
To create a COM component in MATLAB, follow these steps:
1. Enter the following command in MATLAB command window:
> Comtool
The com editing page appears. 2:
2. Create a project: file-> new project ..., 3.
3. Set component properties. In the "component name" field, enter the component name "component". At this time, the class "component" is automatically generated ", enter the class name "huatu", 4,
To facilitate differentiation, select "component" in "classes" and click "Remove" to remove the Class component. Then click "add>" to add the new class huatu. Expected result 5 is displayed. Click "OK". In the following dialog box, select "yes ".
4. Add a file: Select "huatu" in the workspace on the left, click Project-> Add file ..., Select the edited function file huatu. M, 6. Note that the M file must be an M function; otherwise, an error is reported. If it is an M script file, you only need to change it to the m function without input and output parameters.
5. Generate COM components: Click build-> COM Object ..., Result 7.
The COM component has been prepared by MATLAB. The default saving position is: MATLAB installation position \ work \ component.

To call the COM component in VC, follow these steps:
1. Create a dialog box-based MFC (exe) named comhuatu in VC ).
2. Add a button named "Drawing" on the panel, 8.

3. Copy the component_idl.h and component_idl_ I .c files to the project comhuatu directory created by VC. The default directory of the two files is \ work \ component \ SRC
4. Add the above two files to the project: Project-> Add project-> files, select the component_idl.h and component_idl_ I .c files copied to the directory.
5. Copy mwcomtypes. h under/extern/include/to the comhuatu directory of the project and add it to the project. The method is the same as above.
6. Add the header file component_idl.h, component_idl_ I .c, and mwcomtypes FOR THE Program . h, result 9
7. Add Code :< br> coinitialize (null) to the button drawing function );
ihuatu * pimyclass;
hresult hR = cocreateinstance...
the significance of Function Code involves com, and the com-related knowledge involved in calling COM components by VC is being written.
8. Set the pre-compiled header file: Project-> set (shortcut: Alt + F7), select the C/C ++ item precomplied headers, set 11
9. Compile, connect, and run. Result 12 is displayed.
it can be seen that using VC to call COM components to call Matlab is relatively simple, and almost all functions of Matlab are supported, when M files are large, MATCOM is used, Math Library is called, or MCC cannot be used, we recommend that you use it. It should be noted that the above programs cannot be directly run after being copied to another machine, because the COM component is not registered on a new machine. If you want to use it on another machine, you also need to package and install the COM component, and because the program code is very simple, you will not provide the source program, you can do it yourself.
slow!

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.