Comparison of several ways of MATLAB and C # connection

Source: Internet
Author: User
Tags dbx

Http://www.cnblogs.com/ceachy/articles/2153322.html

Use the environment in Visual Studio 2005,matlab 2007a.

The premise: the machine to install the MCR (very abnormal, 100MB~200MB due to the version of the different), or it will compile error.

1.COM

Steps:

MATLAB compilation work
-Mbuild-setup
-Deploytool,matlab Builder for. Net,generic COM Component
-Add m function file (EG:MYFUNC.M), change class name (Eg:mycomclass), build

Registering DLLs
regsvr32 myCom.dll
regsvr32 mwcomutil.dll

VS2005 Call:
-Reference Add COM control
-invoke 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);

Note:
-If the error, and the error is garbled, please open the log yourself to see what, log inside can display the Chinese.
-VS2005 must be installed on the VC, otherwise cannot find MIDL this file, will not compile COM

2.. NET (see Matlab Builder for. NET Help, active code)

This connection is available after the 2006a version of Matlab.

Steps:

MATLAB compilation work
-Mbuild-setup
-Deploytool,matlab Builder for. Net,.net Component
-Add m function file (EG:MYFUNC.M), change class name (Eg:myclass), build
VS2005 Call:
-Reference to have Mwarray (%matlabpath%\toolbox\dotnetbuilder\bin\win32\v2.0) and build DLL
-Use the name space
Using MathWorks.MATLAB.NET.Utility;
Using MathWorks.MATLAB.NET.Arrays;
using DLL name;
-Mwnumericarray is the intermediate class of data in Mwarray and C #, how to use it? How do I pass parameters between C # and Matlab?
Variable delivery of numeric types such as type a.double and int
Mwnumericarray i = null, result = MyDouble;
i=4;
MyClass MyClass = new MyClass (); Instantiation of
result = (Mwnumericarray) myclass.myfunc (i);

B. String (Requires Mwchararray and Mwarray conversions)
Mwchararray FileName = myString;
Mwnumericarray sensitivity;
Sensitivity = (Mwnumericarray) myclass.myalgorithm ((Mwarray) FileName);

C. Arrays of multiple output parameters
Mwnumericarray Out_arr = (mwnumericarray) out_args[1]; Take out the array returned by the first parameter (the lower bound of the array returned by MATLAB is starting at 1)

Remove an element value from an array
Mydouble=out_arr[i]. Toscalardouble ();

As the above example toscalardouble a class of toxxxx and other methods there are many, slowly explore it.

D.result.toarray can turn the matrix returned by Matlab into C # 's n x m array, eg:

double[,] csarray= (double[,]) result. ToArray (Mwarraycomponent.real);

E. Passing an array to MATLAB (as with a method of passing a single value)//April 26 add
double[,] dbx = new double[2, 2] {{1, 2}, {3, 4}};
Mwnumericarray x=dbx;
Myclass.picture (x);

Where picture is a self-written m function, the content is plot (x), used to validate the result of the incoming matrix. Results such as:

3. Time, CPU, memory overhead comparison:

The time is so calculated:
DateTime tst0 = DateTime.Now;
...
DateTime tst1 = DateTime.Now;
TimeSpan dift = tst1-tst0;

Matlab-c# memory/virtual memory overhead (MByte)
-----------------------------------
1 Magic (4) 77.9, 158.4
2 Magic (4) 77.9, 158.4
1 Magic (500) 84.7, 165.2
No interface program 7.4M, 8M

Time Test (s). net/com
-----------------------------------Group1
Instantiation Time 3.2813 3.2813
First Tune Magic (4) 0.0469 0.0625
Second Tuning Magic (5) 0.0000 0.0313
Pass 5x5 matrix 0.1875 not tested
-----------------------------------Group2
Instantiation Time 3.2656 3.3125
First Tune Magic (500) 0.1406 0.1719
Second Tuning Magic (557) 0.0156 0.1250
Pass 500x500 matrix 0.2969 not tested
-----------------------------------Group3
Instantiation time 3.3125 not tested
First Tune Magic (4) 0.0469 not tested
Second tune sumab (0.0156) not tested

Conclusion:
-The first time to run the program after the boot, it takes about 10s of time to instantiate the class, then the instantiation takes 3s of time.
-Consumes memory, occupies time is the process of instantiating MCR!!
-It is best to instantiate the encapsulated class in the front of the program.
-The type conversion of a large matrix consumes a period of time, but is not obvious compared to the small matrix. If the amount of data is really large, it is also a workaround to pass parameters with the file.
-com mode and. NET mode calls are small, but in terms of compile speed,. NET is much faster and seems more stable. (This is the case with my machine, COM compile error but. NET compilation passed quickly, and it was used well)
-Earlier versions of the MCR library are smaller and are estimated to be faster.
-some machines cannot compile COM, possibly Windows XP lacks patches. It is said that some of the services of the "tomato Garden" and other versions of the system are "optimized" and cannot be compiled properly.
-After you update the MATLAB program and copy the DLL to the appropriate folder, you should delete the (DLL name) _MCR folder under the folder, or the program may have an exception loading the DLL.

----------------------------

Commissioning Experience (updated March 30, 2009)

Because MATLAB handles input parameters very flexibly, and C # is very strict on input and output, there is often an issue with the conversion data type error.

The solution is to see the input and output of the C # interface firmly: Matlab function input parameters preferably double type, do not double and float mixed, otherwise the output may be some float some are double.

-------------------------------

Two ways to handle data interfaces (updated July 22, 2009)

The Time test table above gives the time that the value delivery method consumes. The value-passing method consumes a lot of time when the passing parameters are found in the actual use. Therefore, a method of passing parameters or results through a file is proposed. The following is an evaluation of the time between operations and return data consumption in both methods.

Time Test (s) value pass/file
-----------------------------------Group1
Instantiation Time 3.5625 3.5937
First Tune Magic (4) 0.0469 0.0781
Second Tuning Magic (500) 0.1250 0.1406
Pass 500x500 Matrix 0.2812 0.0000 (cannot be measured)

Because the file transfer 500x500 the shaping matrix time is too short to measure, it is changed to 800x800 to evaluate its time. Time to consume: 0.0156

Comparison of several ways of MATLAB and C # connection

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.