[Original] Matlab and. NET Based on type security interface programming entry

Source: Internet
Author: User
Tags net xml visual studio 2010

If these articles are helpful to you and look forward to more open-source components, please do not mean your mouse in your hands. 

[Original sharing] Matlab. NET mixed coding call Figure form http://www.cnblogs.com/asxinyu/archive/2013/04/14/3020813.html [original] Open Source. net xml database introduction and entry http://www.cnblogs.com/asxinyu/archive/2013/03/25/2980086.html [original] On. NET open source and commercial image processing (PSD) component http://www.cnblogs.com/asxinyu/archive/2013/03/21/2972491.html [original ]. NET open source compression component introduction and entry http://www.cnblogs.com/asxinyu/archive/2013/03/05/2943696.html [original] Open Source Word read and write components DocX introduction and entry [information has been sent] http://www.cnblogs.com/asxinyu/archive/2013/02/22/2921861.html [original] C # Open Source lightweight object database NDatabase Introduction [information has been sent keywords: http://www.cnblogs.com/asxinyu/archive/2013/02/20/2918066.html: matlab Mixed Programming, C # Calls Matlab, Matlab and C # mixed programming, MCR, Type-Safe Interfaces

Time: July 22, April 21, 2013, and July 15, May 5, the Demo test was successful.

1. Preface

The Matlab Builder NE toolbox is a toolbox mixed with C #. The help documentation in it is very detailed. It provides 4 cases of Mixed Programming with. NET.

1. Simple Component Integration. Dll assembly is generated through Matlab and called in. NET environment through MWArray components;

2. Another scenario is the use of Figure and hybrid editing in the Web environment, which is similar to 1;

3. Another one is the type-safe interface programming that we will talk about today, as well as the support for WCF and MEF;

4. The last one is used in. NET Remoting technology.

In the beginner's tutorial and in my current mixed compilation development, I only used 1st methods, which is very simple and fast, of course, you must have mastered many basic technologies before you can reach this level. One of the major advantages of interface-based programming today is that it can avoid type conversion, because there are a lot of methods in the past. NET and Matlab type conversion, friends with a solid foundation are easy to confuse. Interface-based programming can avoid many problems, but it also puts forward higher requirements on the basics, and needs to define and function interfaces, and method polymorphism.

The following is a description of the help in Matlab:

2. Simple Description of Type-Safe Interfaces

Different from the requirements for directly compiling a. NET assembly, the use of this technology requires a lot less. NET programmers and requires little Matlab knowledge, but must be proficient in. NET technology,

Previously, due to the incompatibility between the Matlab data type and the basic. NET data type, the following technology is required to enable data communication between the Matlab and. NET programs:

1) Marshal data from. NET input data to a deployed function by creating an MWArray object from native. NET data.

The public functions in a deployed component return MWArray objects.

2) Marshal the output MATLAB data in an MWArray into native. NET data by calling one of the MWArray marshaling methods (ToArray (), for example ).

Therefore, there are many data type conversion procedures using the traditional mixed encoding method. After the Type-Safe Interfaces technology is used, these intermediate processes can be ignored,

Only the. NET Type of relational input and the output. NET Type result are required. Matlab will convert the data internally. Let's take a look at the comparison between the two pictures:

 

Therefore, we can clearly see the advantages of Type-Safe Interfaces:

You avoid training and coding costs associated with teaching end users to work with MWArrays.

You minimize cost of data you must specified Al by either placing MWArray objects in type-safe interfaces or by calling MWArray-based functions in the deployed component.

Flexibility-you mix type-safe interfaces with manual data discovery aling to accommodate data of varying sizes and access patterns.

For example, you may have a few large data objects (images, for example) that wocould incur excess cost to your organization if managed

With a type-safe interface. By mixing type-safe interfaces and manual marshaling, smaller data types can be managed

Automatically with the type-safe interface and your large data can be managed on an as-needed basis.

3. Type-Safe Interfaces case studies

This mixed encoding method can save a lot of time. I think it should be mastered as the original data type conversion method. After you have mastered it, using this method will get twice the result with half the effort. After all, not all problems can be solved using interfaces. Next, let's use a small programming instance to demonstrate the use of the entire process.

Demo environment: Visual Studio 2010, Matlab 2012a,. NET 4.0

Demonstration content: A simple implementation of multiplication and Mixing

1. Create a project of the class library type, named. NET 4.0, as shown in:

1. Add an interface file IMultiply. cs in the above project and write the following code. Because we implement a simple multiplication, we need to add the following interface methods:

1 namespace matlab demo 2 {3 /// <summary> multiplication interface </summary> 4 public interface IMultiply 5 {6/2 number directly multiply 7 double multiply (double x, double y); 8 9 // array multiplication 10 double [] multiply (double [] x, double y); 11 12 // matrix multiplication 13 double [,] multiply (double [,] x, double [,] y); 14} 15}

Note that the interface accessibility must be set to Public. Otherwise, the Matlab will not find the interface prototype during mixed compilation and compilation will fail. Compile the project, compile the project, and obtain the file "Matlab demo. dll" in the bin folder. The steps below this file will be used.

3. Compile the M function and set the mixed compilation project. Let's write a simple multiplication M function m, as shown below:

1 function z = multiply(x, y)2 3     z = x * y;

Note that the name here must be the same as the name of the interface method. Otherwise, how can Matlab get to know it. Then, enter the deploytool command in Matlab, enter the project name TypeSaftDemo, and select the project type :. NET Assembly, this process is the basic process of the "mixed-encoding trilogy". I have seen it in the previous video, but I will not explain it carefully, as shown in:

After confirmation, the mixed compilation project is basically established, a new class DemoTest is created, and the multiply. the m function is added to this class. This is actually a conventional mixing process, which is very simple. After the addition is complete, if it is a common mixed compilation method, you can directly compile it. However, there is another process to set for interface-based mixed compilation, click Settings for the mixed editing project (Settings ...):

For example, open the settings and select the "Type Safe API" tab:

First, select the prepared MATLAB demo in the first process. after selecting the dll interface file, the program will automatically search for available interfaces (public) in the dll. the NET interface drop-down list shows, select what you need, here we are IMultiply, as shown in the MATLAB demo. IMultiply, and then select the class for this interface and the Matlab mixed program, our multiply. m functions in the DemoTest class, such as Wrapped class:

Close the settings. Then compile. If there is no problem, after prompting that the compilation is successful, open the compiled folder. We can see the following three dll files:

Generated three files: TypeSafeDemo. in fact, the dll is the same as the normal mixed encoding method, and can be used in the same way as the previous data type conversion method; and DemoTestIMultiply. dll and TypeSafeDemoNative. dll is used together. Matlab has encapsulated the data conversion process and can be used directly. The first method does not demonstrate how to use interfaces to calculate multiplication.

V. C # Call the computation result of the mixed dll. In the previous section, we have explained the functions of several dll files. The following describes their usage and DemoTestIMultiply. dll and TypeSafeDemoNative. copy the dll together to the C # project of the test project (create a console test project), and add the references of these dll: MWAarray. dll and DemoTestIMultiply. dll and TypeSafeDemoNative. dll. Then add the namespace. The core code is as follows:

1 using System; 2 using System. collections. generic; 3 using System. linq; 4 using System. text; 5 6 using MathWorks. MATLAB. NET. arrays; 7 using MathWorks. MATLAB. NET. utility; 8 9 using TypeSaftDemo; 10 11 namespace TypeSaftTest12 {13 class Program14 {15 static void Main (string [] args) 16 {17 // instantiation of the mixed encoding Interface Class 18 DemoTestIMultiply di = new DemoTestIMultiply (); 19 double [,] a = new double }, {3, 4 }}; 20 double [,] B = new double [2, 2] {6, 7}, {8, 9 }}; 21 // demonstrate the three different calls of the interface 22 var t1 = di. multiply (3, 5); 23 var t2 = di. multiply (new double [] {1, 2, 3}, 5); 24 var t3 = di. multiply (a, B); 25 Console. writeLine ("2 numbers directly multiply:" + t1.ToString (); 26 Console. write ("directly multiply the number of arrays and a single number:"); 27 foreach (var item in t2) Console. write (item. toString () + ""); 28 Console. writeLine (); 29 Console. write ("matrix multiplication:"); 30 foreach (var item in t3) Console. write (item. toString () + ""); 31 Console. readKey (); 32} 33} 34}

The result is shown in:

The above is the detailed process of Mixed Programming Based on Matlab and C # interfaces. Mixed Programming is a very useful tool, but it is really difficult to make good use of it. It has been such a long time since the contact with mixed programming, many people have used this tool, and the vast majority of it is misuse, resulting in a lot of work for no reason. If you want to become a good assistant for your work and study, you must strengthen Matlab and. NET basic learning, only 2 people have a good grasp of the basics, enough to be familiar with the two platforms, you can solve problems encountered in the mixed compilation process more smoothly. When a problem occurs in the compilation process, a lot of experience is required to solve the problem. This is very important. Don't expect to learn this process to solve all the problems. We look forward to the growing support of Mathworks for Mixed Programming of Matlab.

4. Related Resources

1 Type-Safe Interfaces, WCF, and MEF in the User's Guide section of the Matlab Builder NE box

2 Loren Shure blog:

Http://blogs.mathworks.com/loren/2011/06/03/introducing-type-safe-apis-with-builder-ne/

Http://blogs.mathworks.com/loren/2011/06/30/multiple-inputs-and-outputs-in-builder-ne-type-safe-apis/

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.