Finally implement C # Call the Matlab function (zz)

Source: Internet
Author: User
Finally implement C # Call the Matlab function (zz)

. NET is the development direction of Microsoft. In the future, we will inevitably call some MATLAB languages when developing software.

We have been calling MATLAB with C # In the *** figure tonight and finally succeeded.

The main idea is to first make the m function of Matlab into a COM component, and then apply the COM component in C.

The main steps are as follows:
1. Create an M file (test. m)
For example:
Function Test (H, d)
For x =,
If X> d
Y = h;
Hold on;
Plot (x, y)
Elseif x <-d
Y =-h;
Hold on;
Plot (x, y)
Else
Y = H/(d * X );
Hold on;
Plot (x, y)
End
End

2. input the comtool command in MATLAB to open the MATLAB com builder,

Click file-new project to create a new project,

Enter the name in component name and click OK.

Then click Project -- add files to add test. m to the project,

Click build-COM object, and a test1_0.dll will be generated in the comtest \ distrib \ folder (
COM component ),

MATLAB automatically registers the DLL in the Registry during build.

We also need to make preparations to go To the <MATLAB root>/bin/Win32 Directory (on your machine
MATLAB installation path), enter regsvr32 mwcomutil. dll, that is, to register mwcomutil. dll (this DLL
Is the DLL used by any COM component in MATLAB). We can use it when downgrading other compilers.
.

3. C # Implementation call

Create a C # project (I use vs. net2003) and select reference in Solution Explorer on the right.
, Right-click, select Add reference, select COM in the pop-up window, and find test_1_0. dll. Click
Select, and then click OK. At this time, the COM component is added to the project. We can also choose

Then writeProgram

Test. testclassclass ST = new test. testclassclass ();
Object M = 1, n = 2;
St. Test (m, n );
You can.

Then the figure we want to get appears in C #.

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.