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

2025-06-28

144 {

145 mcr.EvaluateFunction(0, \, new Object[]{}); 146 } 147 148

149///

150/// Provides a void output, 1-input Objectinterface to the PlotTest M-function.

151///

152///

153/// M-Documentation:

154/// 编写一个简单的函数,对plot进行简单封装一下 155///

156/// 157///

158publicvoid PlotTest(Object n) 159 {

160 mcr.EvaluateFunction(0, \, n); 161 } 162 163

164///

165/// Provides the standard 0-input Object interface to the PlotTest M-function.

166///

167///

168/// M-Documentation:

169/// 编写一个简单的函数,对plot进行简单封装一下 170///

171///

172///An Array of length \173/// arguments. 174///

175public Object[] PlotTest(int numArgsOut) 176 { 177return mcr.EvaluateFunction(numArgsOut, \, new Object[]{}); 178 } 179 180

181///

182/// Provides the standard 1-input Object interface to the PlotTest M-function.

183///

184///

185/// M-Documentation:

186/// 编写一个简单的函数,对plot进行简单封装一下 187///

188///

189///

190///An Array of length \191/// arguments. 192///

193public Object[] PlotTest(int numArgsOut, Object n) 194 {

195return mcr.EvaluateFunction(numArgsOut, \, n); 196 } 197 198

199///

200/// Provides an interface for the PlotTest function in which the input and output

201/// arguments are specified as an array of Objects. 202///

203///

204/// This method will allocate and return by reference the output argument

205/// array. 206/// M-Documentation:

207/// 编写一个简单的函数,对plot进行简单封装一下 208///

209///

210/// 211/// 212///

213/// arguments 214///

215 [MATLABSignature(\, 1, 0, 0)] 216protectedvoid PlotTest(int numArgsOut, ref Object[] argsOut, Object[] argsIn, params Object[] varArgsIn) 217 {

218 mcr.EvaluateFunctionForTypeSafeCall(\, numArgsOut, ref argsOut, argsIn, varArgsIn); 219 } 220

221///

222/// This method will cause a MATLAB figure window to behave as a modal dialog box.

223/// The method will not return until all the figure windows associated with this

224/// component have been closed. 225///

226///

227/// An application should only call this method when required to keep the

228/// MATLAB figure window from disappearing. Other techniques, such as calling 229/// Console.ReadLine() from the application should be considered where 230/// possible. 231///

232publicvoid WaitForFiguresToDie() 233 {

234 mcr.WaitForFiguresToDie(); 235 } 236 237 238

239#endregion Methods 240

241#region Class Members 242

243privatestatic MWMCR mcr= null; 244

245privatebool disposed= false; 246

247#endregion Class Members 248 } 249 }

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

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

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

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

1/*

2* MATLAB Compiler: 4.17 (R2012a) 3* Date: Mon Sep 09 16:19:01 2013

4* Arguments: \\ 5* \

\

6* \ 7* \\ 8* \

\\ 9*/

10using System;

11using System.Reflection; 12using System.IO;

13using MathWorks.MATLAB.NET.Arrays; 14using MathWorks.MATLAB.NET.Utility;

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

2.关键的静态构造函数

1static TestDemo() 2 {

3if (MWMCR.MCRAppInitialized) 4 {

5 Assembly assembly= Assembly.GetExecutingAssembly(); 6string ctfFilePath= assembly.Location;

7int lastDelimiter= ctfFilePath.LastIndexOf(@\);

8 ctfFilePath= ctfFilePath.Remove(lastDelimiter, (ctfFilePath.Length - lastDelimiter)); 9string ctfFileName = \;

10 Stream embeddedCtfStream = null; 11 String[] resourceStrings = assembly.GetManifestResourceNames();

12foreach (String name in resourceStrings) 13 {

14if (name.Contains(ctfFileName)) 15 {

16 embeddedCtfStream =

assembly.GetManifestResourceStream(name); 17break;

18 } 19 }

20 mcr= new MWMCR(\,ctfFilePath, embeddedCtfStream, true); 21 } 22else

23 {

24thrownew ApplicationException(\initialized\); 25 } 26 }

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

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


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

下一篇:《天作之合》中西合璧婚礼主持词

相关阅读
本类排行
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 7

支付方式:

开通VIP包月会员 特价:29元/月

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