C # mixed programming with MATLAB

Source: Internet
Author: User
Tags dbx

(Vs2005 + MATLAB 7.5) DLL production steps: star -- MATLAB builder. net -- deployment tool -- New -- MATLAB builder. net --. NET component -- Right-click Add file -- find the target M file -- compile

Reference in vs2005: reference the compiled DLL and keep *. CTF in the same folder.

Code
Using Mathworks. MATLAB. net. arrays;
Using Mathworks. MATLAB. net. utility;

 

Code
Test. testclass test = new test. testclass ();

Mwnumericarray is the intermediate class of data in mwarray and C #. how to use it? How to transmit parameters between C # and MATLAB?
A. Variable transfer for numeric types such as double and int
Mwnumericarray I = NULL, result = mydouble;
I = 4;
Myclass = new myclass (); // instantiate
Result = (mwnumericarray) myclass. myfunc (I );

B. String (requires mwchararray and mwarray conversion)
Mwchararray filename = mystring;
Mwnumericarray sensiti.pdf;
Sensitivity = (mwnumericarray) myclass. myalgorithm (mwarray) filename );

C. An array composed of multiple output parameters
Mwnumericarray out_arr = (mwnumericarray) out_args [1]; // retrieve the array returned by the first parameter (the lower bound of the array returned by MATLAB starts from 1)

Extracts an element value from the array.
Mydouble = out_arr [I]. toscalardouble ();

For example, there are many methods such as toxxxx in the toscalardouble class in the above example.

D. Result. toarray can convert the matrix returned by MATLAB to the N x m array of C #. For example:

Double [,] csarray = (double [,]) result. toarray (mwarraycomponent. Real );

E. Pass the array into MATLAB (same as the method for passing a single value)
Double [,] DBX = new double [2, 2] {1, 2}, {3, 4 }};
Mwnumericarray x = DBX;
Myclass. Picture (X );

 

How to receive and return m Files?

Function x = test (H, d) // receives two parameters and returns one

Function [x1, x2] = test (H, d) // returns an array

 

The above is a. net connection, and the following is a COM connection (for compilation speed, the. NET method is much faster and seems stable)

MATLAB Compilation
-Mbuild-Setup
-Deploytool, Matlab builder for. net, generic COM Component
-Add the m function file (eg: myfunc. m), change the class name (eg: mycomclass), and build

Register DLL
Regsvr32 mycom. dll
Regsvr32 mwcomutil. dll

Vs2005 call:
-Reference and COM controls
-Call example
Double [,] arr = NULL;
Object in_a, out_a;
In_a = 500;
Out_a = arr1;
Mycom. mycomclass MC = new mycom. mycomclass ();
MC. myfunc (1, ref out_a, in_a );

 

Example: (extract two parameters returned by MATLAB and convert them to a matrix of C)

Code
Testz. testzclass ST = new testz. testzclass ();
Mwarray [] Max = ST. testz (2 );
Mwnumericarray X1 = (mwnumericarray) Max [0];
Mwnumericarray X2 = (mwnumericarray) Max [1];
Double [,] location1 = (double [,]) x1.toarray (mwarraycomponent. Real );
Double [,] location2 = (double [,]) x2.toarray (mwarraycomponent. Real );
Label1.text = location1 [1, 2]. tostring ();

M file: Code
Function [x1, x2] = testz ()
X1 = [1 2 3;
4 5 6;
7 8 9;];
X2 = [9 8 7;
6 5 4;
3 2 1;];

Related Article

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.