关于matlab与c#混合编程的问题(9)
= 回到目录
4.总结
抛砖引玉,这里只是一个思路,附代码下载吧。
Multiple Inputs and Outputs in Builder NE Type Safe APIs
Posted by Loren Shure, June 30, 2011 Guest blogger Peter Webb returns with another in an occasional series of postings about application deployment. Contents ? ? ? ? Multiple Arguments with Varying Types
Overloaded Functions and Parameter Order in a C# Interface Building and Running the Example Flexibile or Complicated? Multiple Arguments with Varying Types I've got a MATLAB function with multiple inputs and outputs that I'd like to call from C#. Though that may not seem like much of a challange, MATLAB processes function arguments very differently from C#, particularly optional arguments and multiple return values. The behavior of my MATLAB function, polygonal, depends on both the number and types of the inputs. polygonal computesthree different number sequences. Given an integer N for each sequence, it returns the Nth entry in each sequence. Only the first input (tN) is required; the other two are optional. function [t, p, h] = polygonal(tN, pN, hN) In typical MATLAB fashion, if any input is a vector of length K, polygonal returns K elements of that sequence. For each sequence, the numeric type of the input determines the numeric type of the output. If tN is int32, for example, polygonalreturns an int32 t. That's a lot of complexity wrapped up in a single function line. I count at least 36 different ways to call polygonal. How do we manage this complexity in C#? In a word: overloading. Overloaded Functions and Parameter Order in a C# Interface Generally speaking, a function is overloaded if you can call it with different numbers or types of inputs and outputs. In C#, you must write a new function for each set of inputs and outputs. I've implemented five of polygonal's 36 overloads in C# to demonstrate how Builder NE matches C# function declarations to MATLAB functions. The simplest of polygonal's overloads consists of one scalar double input and one scalar double output. In C#: double polygonal(double t); Builder NE maps the first C# input, t to the first MATLAB input (tN, and note the names of the arguments are not considered in the matching process), and the first MATLAB output (t) to the C# function's return value.
Next, I'll declare a function to call polygonal with three scalar inputs, which produces three scalar outputs. Since C# does not permit multiple return values, Builder NE supports C# out parameters in their place. void polygonal(out double t, out double p, out double h, double tN, double pN, double hN); In this function, the outputs appear first, followed by the inputs, and the return type is void. This form is perhaps closest to the structure of the MATLAB function, but Builder NE supports many others. You may map the first MATLAB output to the C# function's return value:
double polygonal(out double p, out double h, double tN, double pN, double hN); out t has vanished, and the return type changed from void to double.
You may also interleave (mix) inputs and outputs. Here, I've placed each input before the output it produces:
void polygonal(double tN, out double t, double pN, out double p, double hN, out double h); Finally, remember that polygonal supports vector inputs and and outputs; here I've requested double[] vector outputs from from double[] vector inputs. In this function the outputs again appear before the inputs, as that's the style I prefer. void polygonal(out double[] t, out double[] p, out double[] h, double[] tN, double[] pN, double[] hN); Building and Running the Example
First, download the source code for this article from MATLAB Central.
As usual, the process of invoking polygonal through a type safe API consists of three steps: ? Build the IFigurate interface DLL.
? Create the Generator .NET assembly and the GeneratorIFigurate type safe interface. ? Compile the main program, PolySeq, after referencing IFigurate and GenerateIFigurate in the PolySeq project.
The file ReadmeParameters.txt contains detailed instructions. Make sure your runtime environment is set up correctly, and then
run PolySeq\\PolySeq\\bin\\Debug\\PolySeq.exe. It callspolygonal through several of the C# interfaces described above and produces the following output:
The 17th triangular number: 153 The 13th triangular number: 91 The 11th pentagonal number: 176 The 19th hexagonal number : 703 Polygonal Numbers N: 3 6 9 ----------------------- Triangular: 6 21 45 Pentagonal: 12 51 117 Hexagonal : 15 66 153 You can generate the same numbers in MATLAB with three calls to polygonal: % 17th triangular number t = polygonal(17); % 13th triangular, 11th pentagonal and 19th hexagonal numbers [t, p, h] = polygonal(13, 11, 19); % 3rd, 6th, and 9th triangular, pentagonal and hexagonal numbers order = [3, 6, 9]; [t, p, h] = polygonal(order, order, order); Flexibile or Complicated?
What do you think of the parameter ordering and function overloading rules? Some of them were dictated by the structure of C# and MATLAB, but some resulted from usability testing and our design judgement. Are they too complicated, or just flexible enough? Let me know here.
Introducing Type Safe APIs with Builder NE 8Posted by Loren Shure, June 3, 2011
Guest blogger Peter Webb returns with another in an occasional series of postings about application deployment.
Contents
? ? ? ? ?
Life is Too Short to Write Boring Code Type Safe APIs Example C# Code
Building and Running the Example More to Come
Life is Too Short to Write Boring Code
I can call MATLAB functions from C# using Builder NE. But MATLAB and C# have very different data types, so I spend a lot of time manually converti …… 此处隐藏:5000字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [政务民生]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字范文