教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 政务民生 >

关于matlab与c#混合编程的问题(10)

来源:网络收集 时间:2025-09-18
导读: So instead, I turn to the type safe APIs generated by Builder NE (as of version 4.0, R2011a), and let Builder NE write this code for me. Type Safe APIs Type Safe APIs allow you to specify a kind of c

So instead, I turn to the type safe APIs generated by Builder NE (as of version 4.0, R2011a), and let Builder NE write this code for me.

Type Safe APIs

Type Safe APIs allow you to specify a kind of contract between your C# code and your MATLAB functions. You describe exactly what kind of data you want to pass back and forth between MATLAB and C#, and Builder NE generates functions for those types only. You give up a little flexiblity (you can't pass just any type of data to your MATLAB function, like you could with the MWArray-based interface), but you gain type safety. Now the C# compiler can tell if you're doing something you shouldn't be, like passing strings to a function that operates on integers. (While this often completes without error, it very seldom produces a useful result.)

In C#, the usual mechanism for establishing such a contract is an interface. An interface defines the names and input and output types of one or more functions, but provides no implemention. We can collect our

three multiply functions into the IMultiplyinterface. By convention, .NET interface names begin with a capitial I.

interface IMultiply { // Scalar multiplication double multiply(double x, double y); // Multiply vector by a scalar, return a vector double[] multiply(double[] x, double y); // Matrix multiplication double[,] multiply(double[,] x, double[,] y); } The interface uses function overloading to take advantage of the polymorphism of the MATLAB function. The functions in the C# interface have the same name as the MATLAB function; Builder NE matches interface functions to MATLAB functions by name and number of arguments. (I'll discuss the details of argument matching in a later article.) To use a type safe API, you'll need:

? ? ?

A MATLAB function

A C# interface that specifies the input and output types of that MATLAB function A C# program that calls the MATLAB function through the C# interface.

The example demonstrates how to incorporate a type safe API into a C# program. In the example, you'll work through 4 basic steps:

? Create a MATLAB function, multiply. ? Define a C# interface IMultiply.

? Compile multiply into a .NET component named Multiply and generate the type safe API MultiplyIMultiply.

? Develop a C# main program that calls multiply through the IMultiply interface.

Since we've already seen the MATLAB code and the C# interface, let's take a look at the C# main program.

Example C# Code

You invoke MATLAB functions from C# by calling the instance methods of a Builder NE-generated component. Builder NE names the type safe API class by combining the names of the MATLAB function component (Multiply) and the C# interface (IMultiply). In this case, MultiplyIMultiply is the name of the type safe API class. Call new to create an instance of the component's type safe API class: // Create an instance of the Multiply component's type safe API. IMultiply m = new Multiply.MultiplyIMultiply(); IMultiply publishes three multiply methods, one of which multiplies a vector and a scalar. Create the input data by declaring C# variables:

// Create a C# vector and a scalar. double[] v = new double[]{ 2.5, 81, 64 }; double s = 11; Finally, compute the product by calling multiply:

// Multiply the vector and the scalar. Note: all inputs and outputs // are native C# types. double[] d = m.multiply(v, s); As usual in C#, passing the wrong type of inputs (if v had been declared as a string for example) results in compile-time errors.

Building and Running the Example

Download the source code from MATLAB Central into a new directory. The download contains

the multiply MATLAB function and two Visual Studio projects. To create a runnable executable, you need to build the C# interface assembly, create adeploytool .NET assembly project for the MATLAB function and then link the main program against these two assemblies. The file ReadmeIntro.txt contains detailed instructions.

The downloaded files compile into Multiplier.exe, a C# program that calls each of the methods in the IMultiply interface and prints the results. Make sure your runtime environment is set up correctly (you need either the MCR or MATLAB'sruntime/ directory on your path) and then locate and run Multiplier.exe from a DOS command window. The output should look something like this: 17 * 3.14159 = 53.40703 [ 2.5 81 64 ] * 11 = [ 27.5 891 704 ] 8 1 6 8 1 6 91 67 67 3 5 7 * 3 5 7 = 67 91 67 4 9 2 4 9 2 67 67 91 To enable other types of multiplcation, say vector times matrix or matrix times scalar, define the appropriate methods inIMultiply and then regenerate the Multiply component.

More to Come

To keep this post from turning into a book, I've discussed only the simplest and most common uses of Builder NE's type safe APIs. Forthcoming articles will reveal the secrets of parameter ordering, address the use of structured types (cell arrays and structures), and demonstrate how type safe APIs enable interprocess communication through the Windows Communication Foundation.

In the meantime, please let me know what you think of this new feature. Will it make your job easier? How could we improve it even futher? Let us know here.

Get the MATLAB code

Published with MATLAB? 7.12

Category:

Deployment

? ? ? ? ? ? ?

Tweet

< Transferring Data Between Two Computers...

Calculating the Area Volume Under... > You can follow any comments to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

8 CommentsOldest to Newest

Alexandre Kozlov replied on June 3rd, 2011 6:23 pm UTC :1 of 8

Isn’t it possigble just to introduce a datatypes for MatLab variables? With – for backward compatibility – a generic default type (lik …… 此处隐藏:7895字,全部文档内容请下载后查看。喜欢就下载吧 ……

关于matlab与c#混合编程的问题(10).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/446919.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)