打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
谈谈Matlab与C/C++或C#的互调用(混合编程)

记得当初一个师姐问我知不知道如何在Matlab里调用C++的程序,还真把我问住了。因为我以前就知道C++调用Matlab的方法,这方面网上资料一大堆。没想到现在自己突发奇想又遇到另外一个问题,Matlab如何调用C#开发的程序。


1、C/C++调用Matlab

这方面资料太多了,随便搜一下“Matlab C++ 混合编程”。


2、Matlab调用C/C++

http://www.xxlinux.com/linux/article/development/soft/20060403/934.html

http://www.codeproject.com/KB/cpp/mexFunction.aspx?msg=734108


3、C#调用Matlab

这个我的博客Matlab分类有。


4、Matlab调用C#


通常是两种方法:COM和非托管化

http://www.mathworks.nl/matlabcentral/newsreader/view_thread/153172

http://topic.csdn.net/u/20090402/16/6db37a24-648c-4f8d-a353-42eab4c4bcbe.html

http://www.sciencenet.cn/m/user_content.aspx?id=304113

 

但是,事实上matlab call dll,这个dll本身就可以是.net开发的,Matlab提供了相关加载和转换的函数。这才是本文的亮点。

 

下面是matlab的相关命令介绍

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/f16-35614.html#brxerx8-1

 

Matlab使用.net数据的例子

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/exampleindex.html

 

下面是展示一些简单的例子和过程。

 

<!-- /* Font Definitions */ @font-face {font-family:宋体; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-alt:SimSun; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;}@font-face {font-family:"/@宋体"; panose-1:2 1 6 0 3 1 1 1 1 1; mso-font-charset:134; mso-generic-font-family:auto; mso-font-pitch:variable; mso-font-signature:3 680460288 22 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal {mso-style-parent:""; margin:0cm; margin-bottom:.0001pt; text-align:justify; text-justify:inter-ideograph; mso-pagination:none; font-size:10.5pt; mso-bidi-font-size:12.0pt; font-family:"Times New Roman"; mso-fareast-font-family:宋体; mso-font-kerning:1.0pt;}h4 {mso-margin-top-alt:auto; margin-right:0cm; mso-margin-bottom-alt:auto; margin-left:0cm; mso-pagination:widow-orphan; mso-outline-level:4; font-size:12.0pt; font-family:宋体; mso-bidi-font-family:宋体; font-weight:bold;}a:link, span.MsoHyperlink {color:blue; text-decoration:underline; text-underline:single;}a:visited, span.MsoHyperlinkFollowed {color:purple; text-decoration:underline; text-underline:single;}p {mso-margin-top-alt:auto; margin-right:0cm; mso-margin-bottom-alt:auto; margin-left:0cm; mso-pagination:widow-orphan; font-size:12.0pt; font-family:宋体; mso-bidi-font-family:宋体;}pre {margin:0cm; margin-bottom:.0001pt; mso-pagination:widow-orphan; tab-stops:45.8pt 91.6pt 137.4pt 183.2pt 229.0pt 274.8pt 320.6pt 366.4pt 412.2pt 458.0pt 503.8pt 549.6pt 595.4pt 641.2pt 687.0pt 732.8pt; font-size:12.0pt; font-family:宋体; mso-bidi-font-family:宋体;}tt {mso-ansi-font-size:12.0pt; mso-bidi-font-size:12.0pt; font-family:宋体; mso-ascii-font-family:宋体; mso-fareast-font-family:宋体; mso-hansi-font-family:宋体; mso-bidi-font-family:宋体;} /* Page Definitions */ @page {mso-page-border-surround-header:no; mso-page-border-surround-footer:no;}@page Section1 {size:595.3pt 841.9pt; margin:72.0pt 90.0pt 72.0pt 90.0pt; mso-header-margin:42.55pt; mso-footer-margin:49.6pt; mso-paper-source:0; layout-grid:15.6pt;}div.Section1 {page:Section1;} /* List Definitions */ @list l0 {mso-list-id:1458570389; mso-list-template-ids:-282954118;}@list l0:level1 {mso-level-number-format:bullet; mso-level-text:?; mso-level-tab-stop:36.0pt; mso-level-number-position:left; text-indent:-18.0pt; mso-ansi-font-size:10.0pt; font-family:Symbol;}ol {margin-bottom:0cm;}ul {margin-bottom:0cm;}-->

SampleProperties Class.  

using System;
namespace netdoc
{
    class SampleProperties
    {
        // string property
        private static string stringField = "The MathWorks";
        public static string stringProp
        {
            get { return stringField; }
            set { stringField = value; }
        }
 
        // read/write property
        private double doubleField = 8.9;
        public double doubleProp
        {
            get { return doubleField; }
            set { doubleField = value; }
        }
 
        // read-only property
        private double readField = 0;
        public double readOnlyProp
        {
            get { return readField; }
        }
 
        // write-only property
        private double writeField = 0;
        public double writeOnlyProp
        {
            set { writeField = value; }
        }
    }
}

Create Object.   Load the assembly and create object obj :

sampleInfo = NET.addAssembly('c:/work/NetSample.dll');

obj = netdoc.SampleProperties

MATLAB displays the object as follows:

obj = 
  netdoc.SampleProperties handle
  Package: netdoc
 
  Properties:
      stringProp: [1x1 System.String]
      doubleProp: 8.9000
    readOnlyProp: 0
 
  Methods, Events, Superclasses

MATLAB displays the stringProp , doubleProp, and readOnlyProp properties, but not the writeOnlyPropproperty. MATLAB also does not display properties defined with the privatekeyword, such as stringField and doubleField .

View Property Values.   To view the value of stringProp , type:

obj.stringProp

MATLAB displays:

ans = 
The MathWorks

Use PropertyValues.   To use the properties, type:

db1 = obj.doubleProp
db2 = obj.readOnlyProp

MATLAB displays:

db1 =
    8.9000
db2 =
     0

Modify PropertyValues.   To modify the properties, type:

obj.doubleProp = 5;
obj.writeOnlyProp = 6;
obj

MATLAB displays (in part):

obj = 
 
  Properties:
      stringProp: [1x1 System.String]
      doubleProp: 5
    readOnlyProp: 0

To modify the static property stringProp , type:

NET.setStaticProperty('netdoc.SampleProperties.stringProp', ...
    'This is a static property');
newVal = obj.stringProp

MATLAB displays:

newVal = 
This is a static property

Using .NET FieldsExample

The SampleFields class defines the followingfields:

·        publicField

·        protectedField

SampleFields Class.  

using System;
namespace netdoc
{
    class SampleFields
    {
        public Int32 publicField;
        protected string protectedField;
    }
}

Create Object.   Load the assembly and create object obj :

NET.addAssembly('c:/work/NetSample.dll')
obj = netdoc.SampleFields;

Modify Field Values.   To set the value of the field publicField , type:

myValue = 3;
obj.publicField = myValue

MATLAB displays:

obj = 
  netdoc.SampleFields handle
  Package: netdoc
 
  Properties:
    publicField: 3
 
  Methods, Events, Superclasses

The publicField property is of type Int32. When you set the value to myValue , which is of MATLAB type double, MATLAB automatically converts the value to the proper type, as described in PassingData to a .NET Object . Type:

class(myValue)
class(obj.publicField)

MATLAB displays:

ans =
double
ans =
int32

 

Limitations to Support of .NET Properties

You cannot pass a cell array to a property, or view protectedproperties in MATLAB.

 

Example — Passing Data To a .NET Assembly将Matlab矩阵转变为.net数组

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/brxe1ww-1.html#brzmvt8-1

 

Example — Converting a Multidimensional .NET Array 将.net数组转变为Matlab矩阵

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_external/brxe1ww-1.html#br1adia-1

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
名家炒股博客
Mathworks Matlab R2013a 注册版 百度网盘高速下载
MATLAB介绍
Matlab官方资料
大拇指看你以后的能不能享福...神准!
Using pointers in Matlab
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服