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

2019-08-20 20:46

= 回到目录

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 converting data from one system to the other. That kind of work is tedious and error-prone -- the C# compiler can't check to ensure that I'm passing the right type of data to my MATLAB functions.

Ideally, I'd like to be able to write MATLAB code that uses MATLAB data types and C# code that uses C# data types, and not have to worry about the conversion between them. Consider a very simple example, a MATLAB multiply function: function z = multiply(x, y) z = x * y; Though it looks simple, multiply behaves very differently depending on the type of its inputs. If the inputs are scalars, the result is a scalar, but if they are matrices, the result is the matrix product. And the numeric data type of the output (int32, double) varies with the type of the input.

In C#, I'd like to call multiply with scalars and matrices, and additionally to to support scalars times vectors. That's three ways to call multiply, and in C#, that means three separate functions: double multiply(double x, double y) // Scalar double[,] multiply(double[,] x, double[,] y) // Matrix double[] multiply(double[] x, double y) // Vector x Scalar So far, that's nothing new -- I can do this today. I write each of these multiply functions myself, filling them up with code that converts .NET scalars, vectors and arrays to MATLAB matrices and vice versa. But it's boring, and I hate being bored.


关于matlab与c#混合编程的问题(9).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:2017会计继续教育考试答案(100分通过非常全)

相关阅读
本类排行
× 注册会员免费下载(下载后可以自由复制和排版)

马上注册会员

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信: QQ: