Matlab.NET混合编程技巧之——直接调用Matlab内置函数(附源码)

时间:2022-02-04 15:50:14

原文:【原创】Matlab.NET混合编程技巧之——直接调用Matlab内置函数(附源码)

在我的上一篇文章【原创】Matlab.NET混编技巧之——找出Matlab内置函数中,已经大概的介绍了matlab内置函数在混合编程中的优点,并通过程序找出了matlab中的大部分内置函数,当然更多人关心是如何像我所说得那样,不用直接编译,就直接在C#中调用这些内置函数。本文就带你揭开这些谜团。

声明,这篇文章是需要一点点混合编程基础的,基本概念和过程要懂一点,如果能简单成功混编一个简单的计算或者绘图例子,可以更容易理解。

1.传统的Matlab.NET混合编程步骤

传统的Matlab.NET混合编程有2种方式:

1)Matlab编写好M函数,利用deploytool编译m函数生成dll,在C#项目中引用并调用;

2)基于接口的编写方式,也是利用deploytool工具,过程繁琐一点,对编程人员素质要求高一点,但不需要进行繁琐的数据类型转换。我的博客有一篇文章专门介绍了这个混合编程方式,也有例子,大家有兴趣的可以看看:http://www.cnblogs.com/asxinyu/archive/2013/05/16/3082299.html

不管上面用哪种方式,Matlab和C#混编的基本步骤,大概都是下面的过程:

1) 编写M函数,并首先在Matlab中测试是正确可以调用的。注意命名规范,注释规范;

2) 使用命令打开 deploytool工具,设置项目名称,选择类型:.NET Assembly,然后新建一个类,并添加编写好的M函数

3) 编译,生成dll,并在C#项目中添加引用(还需要引用对应版本的MWArray),利用对象浏览器查看生成dll的方法结构,并根据Matlab和C#的类型转换规则,进行数据转换即可, 如果是接口的编程,这个过程相对要简单。

2.深入解析传统混编所生成的代码

2.1 第一步:编写M函数,并测试可以使用

为了好我们今天的目的相匹配,特意封装一个简单的内置函数,plot,来画一个简单的图形,如下所示M函数

 function PlotTest(n)
%编写一个简单的函数,对plot进行简单封装一下
plot(:n,:n);
%测试正确,才可以进行下一步工作

注意,混编必须是m函数function的形式才能被调用。上述函数简单测试一下,没有问题(复杂的函数一定要多测试,否则后续调试非常困难)。继续下一步。

2.2 第二步:在Matlab中使用deploytool建立混编项目

在Matlab工作区输入命令:deploytool,然后得到下面界面,输入混编项目的名称,选择存储位置,关键的是类型那里一定要选择".NET Assembly"。如下图所示:

Matlab.NET混合编程技巧之——直接调用Matlab内置函数(附源码)

选择“OK”之后,下一步matlab界面右侧会出现项目解决方案,需要添加类名称和M文件。这个类名称,就是编译完成之后C#项目中的类对象名称,然后添加我们刚才上一步编写的“PlotTest.m”,然后编译即可,如下图所示:

Matlab.NET混合编程技巧之——直接调用Matlab内置函数(附源码)

到此为止,一个常规 简单的Matlab.NET混编已经完成了60%了。编译完成之后,打开“Package”选项卡,即可看到生成的dll文件,然后点击右键,打开文件夹即可,如下图所示:

Matlab.NET混合编程技巧之——直接调用Matlab内置函数(附源码)

2.3 查看混编生成的代码

  这个过程很关键,其实包含很多信息,只不过95%以上的人都没有注意到其实混编生成的dll是有源文件的,通过查看源文件就应该知道混编的原理,只不过这是matlab自动生成 而已。那看看生成的源码吧。

  打开Matlab混编项目的目录,可以看到有2个文件夹,"distrib”,“src”2个文件夹。"distrib"文件夹就是上面图中生成的dll,注意有2个dll,1个是“项目名称.dll”,一个是“项目名称Native.dll”,这2个dll的差别可以通过"distrib"文件夹源码来观察。“distrib”就是源代码的文件夹。如下图所示,src文件夹的文件示意图:

Matlab.NET混合编程技巧之——直接调用Matlab内置函数(附源码)

我们2.2中新建的类名是TestDemo,所以生成的的源码名称也是TestDemo,看看这2个cs文件中的代码,同时类的方法也可以在VS中通过对象浏览器来查看dll有哪些方法以及方法的参数类型。直接贴这2个cs文件的代码,顺便解释和对比下:

TestDemo.cs文件源码:

 /*
* MATLAB Compiler: 4.17 (R2012a)
* Date: Mon Sep 09 16:19:01 2013
* Arguments: "-B" "macro_default" "-W" "dotnet:PlotTest,TestDemo,0.0,private" "-T"
* "link:lib" "-d" "D:\Work\DevelopMent_SVN\Matlab\MatlabBlog\PlotTest\src" "-w"
* "enable:specified_file_mismatch" "-w" "enable:repeated_file" "-w"
* "enable:switch_ignored" "-w" "enable:missing_lib_sentinel" "-w" "enable:demo_license"
* "-v" "class{TestDemo:D:\Work\DevelopMent_SVN\Matlab\MatlabBlog\PlotTest.m}"
*/
using System;
using System.Reflection;
using System.IO;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility; #if SHARED
[assembly: System.Reflection.AssemblyKeyFile(@"")]
#endif namespace PlotTest
{ /// <summary>
/// The TestDemo class provides a CLS compliant, MWArray interface to the M-functions
/// contained in the files:
/// <newpara></newpara>
/// D:\Work\DevelopMent_SVN\Matlab\MatlabBlog\PlotTest.m
/// <newpara></newpara>
/// deployprint.m
/// <newpara></newpara>
/// printdlg.m
/// </summary>
/// <remarks>
/// @Version 0.0
/// </remarks>
public class TestDemo : IDisposable
{
#region Constructors /// <summary internal= "true">
/// The static constructor instantiates and initializes the MATLAB Compiler Runtime
/// instance.
/// </summary>
static TestDemo()
{
if (MWMCR.MCRAppInitialized)
{
Assembly assembly= Assembly.GetExecutingAssembly(); string ctfFilePath= assembly.Location; int lastDelimiter= ctfFilePath.LastIndexOf(@"\"); ctfFilePath= ctfFilePath.Remove(lastDelimiter, (ctfFilePath.Length - lastDelimiter)); string ctfFileName = "PlotTest.ctf"; Stream embeddedCtfStream = null; String[] resourceStrings = assembly.GetManifestResourceNames(); foreach (String name in resourceStrings)
{
if (name.Contains(ctfFileName))
{
embeddedCtfStream = assembly.GetManifestResourceStream(name);
break;
}
}
mcr= new MWMCR("",
ctfFilePath, embeddedCtfStream, true);
}
else
{
throw new ApplicationException("MWArray assembly could not be initialized");
}
} /// <summary>
/// Constructs a new instance of the TestDemo class.
/// </summary>
public TestDemo()
{
} #endregion Constructors #region Finalize /// <summary internal= "true">
/// Class destructor called by the CLR garbage collector.
/// </summary>
~TestDemo()
{
Dispose(false);
} /// <summary>
/// Frees the native resources associated with this object
/// </summary>
public void Dispose()
{
Dispose(true); GC.SuppressFinalize(this);
} /// <summary internal= "true">
/// Internal dispose function
/// </summary>
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
disposed= true; if (disposing)
{
// Free managed resources;
} // Free native resources
}
} #endregion Finalize #region Methods /// <summary>
/// Provides a void output, 0-input MWArrayinterface to the PlotTest M-function.
/// </summary>
/// <remarks>
/// M-Documentation:
/// 编写一个简单的函数,对plot进行简单封装一下
/// </remarks>
///
public void PlotTest()
{
mcr.EvaluateFunction(, "PlotTest", new MWArray[]{});
} /// <summary>
/// Provides a void output, 1-input MWArrayinterface to the PlotTest M-function.
/// </summary>
/// <remarks>
/// M-Documentation:
/// 编写一个简单的函数,对plot进行简单封装一下
/// </remarks>
/// <param name="n">Input argument #1</param>
///
public void PlotTest(MWArray n)
{
mcr.EvaluateFunction(, "PlotTest", n);
} /// <summary>
/// Provides the standard 0-input MWArray interface to the PlotTest M-function.
/// </summary>
/// <remarks>
/// M-Documentation:
/// 编写一个简单的函数,对plot进行简单封装一下
/// </remarks>
/// <param name="numArgsOut">The number of output arguments to return.</param>
/// <returns>An Array of length "numArgsOut" containing the output
/// arguments.</returns>
///
public MWArray[] PlotTest(int numArgsOut)
{
return mcr.EvaluateFunction(numArgsOut, "PlotTest", new MWArray[]{});
} /// <summary>
/// Provides the standard 1-input MWArray interface to the PlotTest M-function.
/// </summary>
/// <remarks>
/// M-Documentation:
/// 编写一个简单的函数,对plot进行简单封装一下
/// </remarks>
/// <param name="numArgsOut">The number of output arguments to return.</param>
/// <param name="n">Input argument #1</param>
/// <returns>An Array of length "numArgsOut" containing the output
/// arguments.</returns>
///
public MWArray[] PlotTest(int numArgsOut, MWArray n)
{
return mcr.EvaluateFunction(numArgsOut, "PlotTest", n);
} /// <summary>
/// This method will cause a MATLAB figure window to behave as a modal dialog box.
/// The method will not return until all the figure windows associated with this
/// component have been closed.
/// </summary>
/// <remarks>
/// An application should only call this method when required to keep the
/// MATLAB figure window from disappearing. Other techniques, such as calling
/// Console.ReadLine() from the application should be considered where
/// possible.</remarks>
///
public void WaitForFiguresToDie()
{
mcr.WaitForFiguresToDie();
} #endregion Methods #region Class Members private static MWMCR mcr= null; private bool disposed= false; #endregion Class Members
}
}

TestDemoNative.cs文件源码:

 /*
* MATLAB Compiler: 4.17 (R2012a)
* Date: Mon Sep 09 16:19:01 2013
* Arguments: "-B" "macro_default" "-W" "dotnet:PlotTest,TestDemo,0.0,private" "-T"
* "link:lib" "-d" "D:\Work\DevelopMent_SVN\Matlab\MatlabBlog\PlotTest\src" "-w"
* "enable:specified_file_mismatch" "-w" "enable:repeated_file" "-w"
* "enable:switch_ignored" "-w" "enable:missing_lib_sentinel" "-w" "enable:demo_license"
* "-v" "class{TestDemo:D:\Work\DevelopMent_SVN\Matlab\MatlabBlog\PlotTest.m}"
*/
using System;
using System.Reflection;
using System.IO;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility; #if SHARED
[assembly: System.Reflection.AssemblyKeyFile(@"")]
#endif namespace PlotTestNative
{ /// <summary>
/// The TestDemo class provides a CLS compliant, Object (native) interface to the
/// M-functions contained in the files:
/// <newpara></newpara>
/// D:\Work\DevelopMent_SVN\Matlab\MatlabBlog\PlotTest.m
/// <newpara></newpara>
/// deployprint.m
/// <newpara></newpara>
/// printdlg.m
/// </summary>
/// <remarks>
/// @Version 0.0
/// </remarks>
public class TestDemo : IDisposable
{
#region Constructors /// <summary internal= "true">
/// The static constructor instantiates and initializes the MATLAB Compiler Runtime
/// instance.
/// </summary>
static TestDemo()
{
if (MWMCR.MCRAppInitialized)
{
Assembly assembly= Assembly.GetExecutingAssembly(); string ctfFilePath= assembly.Location; int lastDelimiter= ctfFilePath.LastIndexOf(@"\"); ctfFilePath= ctfFilePath.Remove(lastDelimiter, (ctfFilePath.Length - lastDelimiter)); string ctfFileName = "PlotTest.ctf"; Stream embeddedCtfStream = null; String[] resourceStrings = assembly.GetManifestResourceNames(); foreach (String name in resourceStrings)
{
if (name.Contains(ctfFileName))
{
embeddedCtfStream = assembly.GetManifestResourceStream(name);
break;
}
}
mcr= new MWMCR("",
ctfFilePath, embeddedCtfStream, true);
}
else
{
throw new ApplicationException("MWArray assembly could not be initialized");
}
} /// <summary>
/// Constructs a new instance of the TestDemo class.
/// </summary>
public TestDemo()
{
} #endregion Constructors #region Finalize /// <summary internal= "true">
/// Class destructor called by the CLR garbage collector.
/// </summary>
~TestDemo()
{
Dispose(false);
} /// <summary>
/// Frees the native resources associated with this object
/// </summary>
public void Dispose()
{
Dispose(true); GC.SuppressFinalize(this);
} /// <summary internal= "true">
/// Internal dispose function
/// </summary>
protected virtual void Dispose(bool disposing)
{
if (!disposed)
{
disposed= true; if (disposing)
{
// Free managed resources;
} // Free native resources
}
} #endregion Finalize #region Methods /// <summary>
/// Provides a void output, 0-input Objectinterface to the PlotTest M-function.
/// </summary>
/// <remarks>
/// M-Documentation:
/// 编写一个简单的函数,对plot进行简单封装一下
/// </remarks>
///
public void PlotTest()
{
mcr.EvaluateFunction(, "PlotTest", new Object[]{});
} /// <summary>
/// Provides a void output, 1-input Objectinterface to the PlotTest M-function.
/// </summary>
/// <remarks>
/// M-Documentation:
/// 编写一个简单的函数,对plot进行简单封装一下
/// </remarks>
/// <param name="n">Input argument #1</param>
///
public void PlotTest(Object n)
{
mcr.EvaluateFunction(, "PlotTest", n);
} /// <summary>
/// Provides the standard 0-input Object interface to the PlotTest M-function.
/// </summary>
/// <remarks>
/// M-Documentation:
/// 编写一个简单的函数,对plot进行简单封装一下
/// </remarks>
/// <param name="numArgsOut">The number of output arguments to return.</param>
/// <returns>An Array of length "numArgsOut" containing the output
/// arguments.</returns>
///
public Object[] PlotTest(int numArgsOut)
{
return mcr.EvaluateFunction(numArgsOut, "PlotTest", new Object[]{});
} /// <summary>
/// Provides the standard 1-input Object interface to the PlotTest M-function.
/// </summary>
/// <remarks>
/// M-Documentation:
/// 编写一个简单的函数,对plot进行简单封装一下
/// </remarks>
/// <param name="numArgsOut">The number of output arguments to return.</param>
/// <param name="n">Input argument #1</param>
/// <returns>An Array of length "numArgsOut" containing the output
/// arguments.</returns>
///
public Object[] PlotTest(int numArgsOut, Object n)
{
return mcr.EvaluateFunction(numArgsOut, "PlotTest", n);
} /// <summary>
/// Provides an interface for the PlotTest function in which the input and output
/// arguments are specified as an array of Objects.
/// </summary>
/// <remarks>
/// This method will allocate and return by reference the output argument
/// array.<newpara></newpara>
/// M-Documentation:
/// 编写一个简单的函数,对plot进行简单封装一下
/// </remarks>
/// <param name="numArgsOut">The number of output arguments to return</param>
/// <param name= "argsOut">Array of Object output arguments</param>
/// <param name= "argsIn">Array of Object input arguments</param>
/// <param name= "varArgsIn">Array of Object representing variable input
/// arguments</param>
///
[MATLABSignature("PlotTest", , , )]
protected void PlotTest(int numArgsOut, ref Object[] argsOut, Object[] argsIn, params Object[] varArgsIn)
{
mcr.EvaluateFunctionForTypeSafeCall("PlotTest", numArgsOut, ref argsOut, argsIn, varArgsIn);
} /// <summary>
/// This method will cause a MATLAB figure window to behave as a modal dialog box.
/// The method will not return until all the figure windows associated with this
/// component have been closed.
/// </summary>
/// <remarks>
/// An application should only call this method when required to keep the
/// MATLAB figure window from disappearing. Other techniques, such as calling
/// Console.ReadLine() from the application should be considered where
/// possible.</remarks>
///
public void WaitForFiguresToDie()
{
mcr.WaitForFiguresToDie();
} #endregion Methods #region Class Members private static MWMCR mcr= null; private bool disposed= false; #endregion Class Members
}
}

对比大家就可以发现,只不过一个更加傻瓜化,参数都是Object了,其实这样反而不好,增加了类型转换的代价,如果知道,为何不给一个正确的给他呢。关于这2个dll的速度,曾经听说过是有差别的,博客园有人给过测试,我没实际测试过,还是习惯用传统的TestDemo.cs,因为参数类型都是Object,不直观,出了问题也有点头疼。

2.4 上述Matlab自动生成代码的要点

  后面某些类或者方法的XML注释就不说了,自动生成的东西,可以对照M文件的注释来看。

1.首先看第一段的注释信息:

 /*
* MATLAB Compiler: 4.17 (R2012a)
* Date: Mon Sep 09 16:19:01 2013
* Arguments: "-B" "macro_default" "-W" "dotnet:PlotTest,TestDemo,0.0,private" "-T"
* "link:lib" "-d" "D:\Work\DevelopMent_SVN\Matlab\MatlabBlog\PlotTest\src" "-w"
* "enable:specified_file_mismatch" "-w" "enable:repeated_file" "-w"
* "enable:switch_ignored" "-w" "enable:missing_lib_sentinel" "-w" "enable:demo_license"
* "-v" "class{TestDemo:D:\Work\DevelopMent_SVN\Matlab\MatlabBlog\PlotTest.m}"
*/
using System;
using System.Reflection;
using System.IO;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;

上面这段信息主要是说明当前Matlab编译器的版本,因为编译的版本和部署的MCR版本必须对应起来,否则是不能运行的。然后有编译日期,以及编译的参数,类名以及M函数的地址等信息,其实知道这些参数,理论上是可以自己在程序里面调用Matlab的编译器进行编译工作的,只不过比较复杂,能力有限,研究不下去。下面的引用大家应该明白,这个是MWArray.dll里面的命名空间,所以混编的项目都要引用对应版本的MWArray.dll

2.关键的静态构造函数

 static TestDemo()
{
if (MWMCR.MCRAppInitialized)
{
Assembly assembly= Assembly.GetExecutingAssembly();
string ctfFilePath= assembly.Location;
int lastDelimiter= ctfFilePath.LastIndexOf(@"\");
ctfFilePath= ctfFilePath.Remove(lastDelimiter, (ctfFilePath.Length - lastDelimiter));
string ctfFileName = "PlotTest.ctf";
Stream embeddedCtfStream = null;
String[] resourceStrings = assembly.GetManifestResourceNames();
foreach (String name in resourceStrings)
{
if (name.Contains(ctfFileName))
{
embeddedCtfStream = assembly.GetManifestResourceStream(name);
break;
}
}
mcr= new MWMCR("",ctfFilePath, embeddedCtfStream, true);
}
else
{
throw new ApplicationException("MWArray assembly could not be initialized");
}
}

  如果有一些C#的开发和编程经验看上面的代码问题应该不大,否则还真有点难说清楚,简单的说几个要点:

  1)这个构造函数的作用主要是检测MCR对象是否初始化,如果没有,则寻找程序集,并拼接资源的位置,正确进行初始化

  2) 上面的ctf其实是包含在dll程序集里面的,以资源的形式,这个文件是核心,它才真正的包括了Matlab编译之后的,MCR可以运行的中间程序。

  3) 必须要合法正确的ctf文件和dll文件才能 正确的初始化mcr对象,合法的意思,是Matlab内部有校验机制,包括对相关版本,在以前的版本生成文件中,很明显,现在2012a里面都隐藏起来了,不过要求都一样。

  4) 上面方法其实已经间接的告诉了我们怎么初始化mcr对象,有了mcr对象,一切都好办了,因为它才是MCR的核心。

3.PlotTest封装的方法代码

     public void PlotTest()
{
mcr.EvaluateFunction(, "PlotTest", new MWArray[]{});
}
public void PlotTest(MWArray n)
{
mcr.EvaluateFunction(, "PlotTest", n);
}
public MWArray[] PlotTest(int numArgsOut)
{
return mcr.EvaluateFunction(numArgsOut, "PlotTest", new MWArray[]{});
}
public MWArray[] PlotTest(int numArgsOut, MWArray n)
{
return mcr.EvaluateFunction(numArgsOut, "PlotTest", n);
}

  看了字段代码,再对应mcr的初始化,其实都很明朗了。通过mcr的EvaluateFunction来调用M函数。上面的代码有几个重载方法,可以实用很多不同的情况,有时候,这些方法的个数会更多,其实没多大必要,也可以自己编译一下,把没用的删掉,保留少数几个有用的即可。同时也可以看到,这里直接通过字符串来传递函数名称的,因此必须保证这个函数能被mcr搜索到。比如我们这里的"PlotTest"这个函数其实就包含了ctf文件中(注意ctf文件是可以和dll分开的,在混编项目里可以设置)。  

3.上述代码到内置函数的调用

  上述已经讲解了整个mcr调用的过程,其实就是通过mcr的EvaluateFunction来调用M函数,但要保证对于的函数名称在mcr搜索的范围内。那么我们是不是可以假设:内置函数都在MCR内部,应该是可以搜索到的,那么把上面的函数名称换一下,是不是也是可行的。这个假设也是我最早接触时候的想法,有了假设,当然要去验证。现在看来这个当然是肯定的,那么不妨重新演示一遍。过程不详细讲了,代码也有注释,混编要引用的MWArray.dll和命名空间也不提了,看代码:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.IO; using MathWorks.MATLAB.NET.Utility;
using MathWorks.MATLAB.NET.Arrays; namespace BuildInFunctionDemo
{
class Program
{
static MWMCR mcr;
static void Main(string[] args)
{
#region 首先使用PlotTest.dll来初始化mcr,因为这个dll是混编“合法”产生的,只有这样才能顺利启动mcr
if (MWMCR.MCRAppInitialized)
{
string path = Path.Combine(System.Environment.CurrentDirectory, "PlotTest.dll");
Assembly assembly = Assembly.LoadFile(path);
string ctfFilePath = assembly.Location;
int lastDelimiter = ctfFilePath.LastIndexOf(@"\");
ctfFilePath = ctfFilePath.Remove(lastDelimiter, (ctfFilePath.Length - lastDelimiter));
string ctfFileName = "PlotTest.ctf";
Stream embeddedCtfStream = null;
String[] resourceStrings = assembly.GetManifestResourceNames(); foreach (String name in resourceStrings)
{
if (name.Contains(ctfFileName))
{
embeddedCtfStream = assembly.GetManifestResourceStream(name);
break;
}
}
mcr = new MWMCR("",ctfFilePath, embeddedCtfStream, true);
}
else
{
throw new ApplicationException("MWArray assembly could not be initialized");
}
#endregion #region 直接调用混编dll中的封装函数进行测试
mcr.EvaluateFunction(, "PlotTest", ); //注意这里要断点调试才能看到效果哦,因为mcr会把图绘制在一个Figure上面,
//后面的会覆盖前面的,这里暂停一下,可以看前面的效果
//下面就是直接调用matlab的plot函数的效果
MWNumericArray x = new double[]{,,,,};
MWNumericArray y = new double[]{,,2.8,5.3,4.7};
mcr.EvaluateFunction(, "plot",x,y );
#endregion Console.ReadKey();
}
}
}

唯一要注意的就是50-52的说明,要加断点看2次绘制的效果。分别截图如下:
Matlab.NET混合编程技巧之——直接调用Matlab内置函数(附源码)Matlab.NET混合编程技巧之——直接调用Matlab内置函数(附源码)=

4.总结

  抛砖引玉,这里只是一个思路,附代码下载吧。

http://www.kuaipan.cn/file/id_4279479643944070.htm