关于matlab与c#混合编程的问题(10)
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字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [政务民生]2013年公共基础知识热点问题(七)
- [政务民生]检验检测机构资质认定评审准则及释义20
- [政务民生]关于印发重庆市房屋建筑和市政基础设施
- [政务民生]1、隧道洞身开挖支护施工技术交底书
- [政务民生]2015年山东省17地市中考语文试题分类汇
- [政务民生]2-高级会计师资格考试和评审流程图
- [政务民生]2018版中国清分机行业发展分析及前景策
- [政务民生]新课改高中政治探究
- [政务民生]2018-2024年中国新型组合房屋行业投资
- [政务民生]2015年上海市春季高考数学模拟试卷五
- [政务民生]灌砂法及环刀法测压实度(带计算过程)
- [政务民生]运筹学实验2求解非线性规划
- [政务民生]劝学、逍遥游默写(教师卷)
- [政务民生]《运筹学》 - 期末考试 - 试卷A - 答案
- [政务民生]八年级英语下册 Module 6 Hobbies测试
- [政务民生]2019年宪法知识竞赛试题库100题(含答
- [政务民生]自动化英文文献翻译
- [政务民生]公文格式实施细则
- [政务民生]高一地理上册课堂跟踪练习题6
- [政务民生]会计继续教育习题及答案
- 第三章 无约束最优化方法
- 泛读教程第三册答案
- 魏晋南北朝文学
- 幂的运算复习题
- 城市环境问题的成因与治理策略_以社会
- 钢结构行业产业链及竞争分析研究
- 新型热塑性弹性体增韧聚丙烯的研究
- 中国旅游地理B卷试题及答案
- (苏教版)五年级数学上册第三单元测试卷
- 不稳定性心绞痛诊断与治疗
- 俞氏国际后勤职能部门绩效考核办法
- GB7258-2017新标准考试题含答案
- 小学生汉字听写比赛活动方案
- 1.3《平抛运动》学案 教科版必修2
- 2011香港特别行政区公务员考试复习资料
- 考虑水力条件变化的城市给水管网可靠性
- 表面活性剂在油田开发和生产中的应用
- ITT内部培训资料-FI端吸泵的介绍
- 文明守纪,从我做起学生发言稿
- 初中读《聊斋志异》心得体会800字范文