关于matlab与c#混合编程的问题(7)
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///
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///
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.
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对象是否初始化,如果没有,则寻找程序 …… 此处隐藏:2031字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [政务民生]2013年公共基础知识热点问题(七)
- [政务民生]检验检测机构资质认定评审准则及释义20
- [政务民生]关于印发重庆市房屋建筑和市政基础设施
- [政务民生]1、隧道洞身开挖支护施工技术交底书
- [政务民生]2015年山东省17地市中考语文试题分类汇
- [政务民生]2-高级会计师资格考试和评审流程图
- [政务民生]2018版中国清分机行业发展分析及前景策
- [政务民生]新课改高中政治探究
- [政务民生]2018-2024年中国新型组合房屋行业投资
- [政务民生]2015年上海市春季高考数学模拟试卷五
- [政务民生]灌砂法及环刀法测压实度(带计算过程)
- [政务民生]运筹学实验2求解非线性规划
- [政务民生]劝学、逍遥游默写(教师卷)
- [政务民生]《运筹学》 - 期末考试 - 试卷A - 答案
- [政务民生]八年级英语下册 Module 6 Hobbies测试
- [政务民生]2019年宪法知识竞赛试题库100题(含答
- [政务民生]自动化英文文献翻译
- [政务民生]公文格式实施细则
- [政务民生]高一地理上册课堂跟踪练习题6
- [政务民生]会计继续教育习题及答案
- 第三章 无约束最优化方法
- 泛读教程第三册答案
- 魏晋南北朝文学
- 幂的运算复习题
- 城市环境问题的成因与治理策略_以社会
- 钢结构行业产业链及竞争分析研究
- 新型热塑性弹性体增韧聚丙烯的研究
- 中国旅游地理B卷试题及答案
- (苏教版)五年级数学上册第三单元测试卷
- 不稳定性心绞痛诊断与治疗
- 俞氏国际后勤职能部门绩效考核办法
- GB7258-2017新标准考试题含答案
- 小学生汉字听写比赛活动方案
- 1.3《平抛运动》学案 教科版必修2
- 2011香港特别行政区公务员考试复习资料
- 考虑水力条件变化的城市给水管网可靠性
- 表面活性剂在油田开发和生产中的应用
- ITT内部培训资料-FI端吸泵的介绍
- 文明守纪,从我做起学生发言稿
- 初中读《聊斋志异》心得体会800字范文