C # Calls the MATLAB program

Source: Internet
Author: User

1. Create an M file (test. m) in MATLAB)

For example:
Function Test (H, d)
For x =,
  If X & gt; d
      Y = h;
      Hold on;
      Plot (x, y)
  Elseif X & lt;-d
      Y =-h;
      Hold on;
      Plot (x, y)
  Else
      Y = H/(d * X );
      Hold on;
      Plot (x, y)
  End
End

Enter the deploytool command in MATLAB r2007a (enter comtool in MATLAB) to open the MATLAB build. create a project in the. NET window. In this project, add files to test. M file, build to generate the COM component, which is test. DLL is the COM component that we will reference in C.

MATLAB automatically registers the DLL in the Registry during build.

However, we still need to make preparations before, open the Start Menu "run", enter regsvr32 "Matlab installation path \ bin \ Win32 \ mwcomutil. DLL ", that is, for mwcomutil. DLL registration (This dll is used by any COM component of MATLAB). We can use it when downgrading other compilers.

2. You can call the DLL file in C # as follows:

"Reference" right-click, select Browse, find test. dll, and Add.
"Reference" right-click, select Browse, find mwarray. dll (MATLAB installation path \ toolbox \ dotnetbuilder \ bin \ Win32 \ V2.0 \ mwarray. dll), and Add.
Write the following code:
           Test. testclass ST = new test. testclass ();
           Mathworks. MATLAB. net. arrays. mwarray M = 1, n = 2;
           St. Test (m, n );

 

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.