记得当初一个师姐问我知不知道如何在Matlab里调用C++的程序,还真把我问住了。因为我以前就知道C++调用Matlab的方法,这方面网上资料一大堆。没想到现在自己突发奇想又遇到另外一个问题,Matlab如何调用C#开发的程序。
1、C/C++调用Matlab
这方面资料太多了,随便搜一下“Matlab C++ 混合编程”。
2、Matlab调用C/C++
3、C#调用Matlab
这个我的博客Matlab分类有。
4、Matlab调用C#
通常是两种方法:COM和非托管化
但是,事实上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
下面是展示一些简单的例子和过程。
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 writeOnlyProp
property. MATLAB also does not display properties defined with the private
keyword, such as stringField
and doubleField
.
View Property Values.
To view the value of stringProp
, type:
obj.stringProp
MATLAB displays:
ans =
The MathWorks
Use Property
Values.
To use the properties, type:
db1 = obj.doubleProp
db2 = obj.readOnlyProp
MATLAB displays:
db1 =
8.9000
db2 =
0
Modify Property
Values.
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 Fields
Example
The SampleFields
class defines the following
fields:
·
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 Passing
Data 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 protected
properties 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