【原創】Matlab.NET混合程式設計技巧之直接呼叫Matlab內建函式

資料之巔發表於2013-09-12

              本部落格所有文章分類的總目錄:【總目錄】本部落格博文總目錄-實時更新

      Matlab和C#混合程式設計文章目錄 :【目錄】Matlab和C#混合程式設計文章目錄

  在我的上一篇文章【原創】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函式    

1 function PlotTest(n)
2 %編寫一個簡單的函式,對plot進行簡單封裝一下
3 plot(1:n,1:n);
4 %測試正確,才可以進行下一步工作

注意,混編必須是m函式function的形式才能被呼叫。上述函式簡單測試一下,沒有問題(複雜的函式一定要多測試,否則後續除錯非常困難)。繼續下一步。

2.2 第二步:在Matlab中使用deploytool建立混編專案

 在Matlab工作區輸入命令:deploytool,然後得到下面介面,輸入混編專案的名稱,選擇儲存位置,關鍵的是型別那裡一定要選擇".NET Assembly"。如下圖所示:

選擇“OK”之後,下一步matlab介面右側會出現專案解決方案,需要新增類名稱和M檔案。這個類名稱,就是編譯完成之後C#專案中的類物件名稱,然後新增我們剛才上一步編寫的“PlotTest.m”,然後編譯即可,如下圖所示:

到此為止,一個常規 簡單的Matlab.NET混編已經完成了60%了。編譯完成之後,開啟“Package”選項卡,即可看到生成的dll檔案,然後點選右鍵,開啟資料夾即可,如下圖所示:

2.3 檢視混編生成的程式碼

  這個過程很關鍵,其實包含很多資訊,只不過95%以上的人都沒有注意到其實混編生成的dll是有原始檔的,通過檢視原始檔就應該知道混編的原理,只不過這是matlab自動生成 而已。那看看生成的原始碼吧。

  開啟Matlab混編專案的目錄,可以看到有2個資料夾,"distrib”,“src”2個資料夾。"distrib"資料夾就是上面圖中生成的dll,注意有2個dll,1個是“專案名稱.dll”,一個是“專案名稱Native.dll”,這2個dll的差別可以通過"distrib"資料夾原始碼來觀察。“distrib”就是原始碼的資料夾。如下圖所示,src資料夾的檔案示意圖:

 我們2.2中新建的類名是TestDemo,所以生成的的原始碼名稱也是TestDemo,看看這2個cs檔案中的程式碼,同時類的方法也可以在VS中通過物件瀏覽器來檢視dll有哪些方法以及方法的引數型別。直接貼這2個cs檔案的程式碼,順便解釋和對比下:

TestDemo.cs檔案原始碼:

  1 /*
  2 * MATLAB Compiler: 4.17 (R2012a)
  3 * Date: Mon Sep 09 16:19:01 2013
  4 * Arguments: "-B" "macro_default" "-W" "dotnet:PlotTest,TestDemo,0.0,private" "-T"
  5 * "link:lib" "-d" "D:\Work\DevelopMent_SVN\Matlab\MatlabBlog\PlotTest\src" "-w"
  6 * "enable:specified_file_mismatch" "-w" "enable:repeated_file" "-w"
  7 * "enable:switch_ignored" "-w" "enable:missing_lib_sentinel" "-w" "enable:demo_license"
  8 * "-v" "class{TestDemo:D:\Work\DevelopMent_SVN\Matlab\MatlabBlog\PlotTest.m}" 
  9 */
 10 using System;
 11 using System.Reflection;
 12 using System.IO;
 13 using MathWorks.MATLAB.NET.Arrays;
 14 using MathWorks.MATLAB.NET.Utility;
 15 
 16 #if SHARED
 17 [assembly: System.Reflection.AssemblyKeyFile(@"")]
 18 #endif
 19 
 20 namespace PlotTest
 21 {
 22 
 23   /// <summary>
 24   /// The TestDemo class provides a CLS compliant, MWArray interface to the M-functions
 25   /// contained in the files:
 26   /// <newpara></newpara>
 27   /// D:\Work\DevelopMent_SVN\Matlab\MatlabBlog\PlotTest.m
 28   /// <newpara></newpara>
 29   /// deployprint.m
 30   /// <newpara></newpara>
 31   /// printdlg.m
 32   /// </summary>
 33   /// <remarks>
 34   /// @Version 0.0
 35   /// </remarks>
 36   public class TestDemo : IDisposable
 37   {
 38     #region Constructors
 39 
 40     /// <summary internal= "true">
 41     /// The static constructor instantiates and initializes the MATLAB Compiler Runtime
 42     /// instance.
 43     /// </summary>
 44     static TestDemo()
 45     {
 46       if (MWMCR.MCRAppInitialized)
 47       {
 48         Assembly assembly= Assembly.GetExecutingAssembly();
 49 
 50         string ctfFilePath= assembly.Location;
 51 
 52         int lastDelimiter= ctfFilePath.LastIndexOf(@"\");
 53 
 54         ctfFilePath= ctfFilePath.Remove(lastDelimiter, (ctfFilePath.Length - lastDelimiter));
 55 
 56         string ctfFileName = "PlotTest.ctf";
 57 
 58         Stream embeddedCtfStream = null;
 59 
 60         String[] resourceStrings = assembly.GetManifestResourceNames();
 61 
 62         foreach (String name in resourceStrings)
 63         {
 64           if (name.Contains(ctfFileName))
 65           {
 66             embeddedCtfStream = assembly.GetManifestResourceStream(name);
 67             break;
 68           }
 69         }
 70         mcr= new MWMCR("",
 71                        ctfFilePath, embeddedCtfStream, true);
 72       }
 73       else
 74       {
 75         throw new ApplicationException("MWArray assembly could not be initialized");
 76       }
 77     }
 78 
 79 
 80     /// <summary>
 81     /// Constructs a new instance of the TestDemo class.
 82     /// </summary>
 83     public TestDemo()
 84     {
 85     }
 86 
 87 
 88     #endregion Constructors
 89 
 90     #region Finalize
 91 
 92     /// <summary internal= "true">
 93     /// Class destructor called by the CLR garbage collector.
 94     /// </summary>
 95     ~TestDemo()
 96     {
 97       Dispose(false);
 98     }
 99 
100 
101     /// <summary>
102     /// Frees the native resources associated with this object
103     /// </summary>
104     public void Dispose()
105     {
106       Dispose(true);
107 
108       GC.SuppressFinalize(this);
109     }
110 
111 
112     /// <summary internal= "true">
113     /// Internal dispose function
114     /// </summary>
115     protected virtual void Dispose(bool disposing)
116     {
117       if (!disposed)
118       {
119         disposed= true;
120 
121         if (disposing)
122         {
123           // Free managed resources;
124         }
125 
126         // Free native resources
127       }
128     }
129 
130 
131     #endregion Finalize
132 
133     #region Methods
134 
135     /// <summary>
136     /// Provides a void output, 0-input MWArrayinterface to the PlotTest M-function.
137     /// </summary>
138     /// <remarks>
139     /// M-Documentation:
140     /// 編寫一個簡單的函式,對plot進行簡單封裝一下
141     /// </remarks>
142     ///
143     public void PlotTest()
144     {
145       mcr.EvaluateFunction(0, "PlotTest", new MWArray[]{});
146     }
147 
148 
149     /// <summary>
150     /// Provides a void output, 1-input MWArrayinterface to the PlotTest M-function.
151     /// </summary>
152     /// <remarks>
153     /// M-Documentation:
154     /// 編寫一個簡單的函式,對plot進行簡單封裝一下
155     /// </remarks>
156     /// <param name="n">Input argument #1</param>
157     ///
158     public void PlotTest(MWArray n)
159     {
160       mcr.EvaluateFunction(0, "PlotTest", n);
161     }
162 
163 
164     /// <summary>
165     /// Provides the standard 0-input MWArray interface to the PlotTest M-function.
166     /// </summary>
167     /// <remarks>
168     /// M-Documentation:
169     /// 編寫一個簡單的函式,對plot進行簡單封裝一下
170     /// </remarks>
171     /// <param name="numArgsOut">The number of output arguments to return.</param>
172     /// <returns>An Array of length "numArgsOut" containing the output
173     /// arguments.</returns>
174     ///
175     public MWArray[] PlotTest(int numArgsOut)
176     {
177       return mcr.EvaluateFunction(numArgsOut, "PlotTest", new MWArray[]{});
178     }
179 
180 
181     /// <summary>
182     /// Provides the standard 1-input MWArray interface to the PlotTest M-function.
183     /// </summary>
184     /// <remarks>
185     /// M-Documentation:
186     /// 編寫一個簡單的函式,對plot進行簡單封裝一下
187     /// </remarks>
188     /// <param name="numArgsOut">The number of output arguments to return.</param>
189     /// <param name="n">Input argument #1</param>
190     /// <returns>An Array of length "numArgsOut" containing the output
191     /// arguments.</returns>
192     ///
193     public MWArray[] PlotTest(int numArgsOut, MWArray n)
194     {
195       return mcr.EvaluateFunction(numArgsOut, "PlotTest", n);
196     }
197 
198 
199 
200     /// <summary>
201     /// This method will cause a MATLAB figure window to behave as a modal dialog box.
202     /// The method will not return until all the figure windows associated with this
203     /// component have been closed.
204     /// </summary>
205     /// <remarks>
206     /// An application should only call this method when required to keep the
207     /// MATLAB figure window from disappearing.  Other techniques, such as calling
208     /// Console.ReadLine() from the application should be considered where
209     /// possible.</remarks>
210     ///
211     public void WaitForFiguresToDie()
212     {
213       mcr.WaitForFiguresToDie();
214     }
215 
216 
217 
218     #endregion Methods
219 
220     #region Class Members
221 
222     private static MWMCR mcr= null;
223 
224     private bool disposed= false;
225 
226     #endregion Class Members
227   }
228 }
View Code

TestDemoNative.cs檔案原始碼:

  1 /*
  2 * MATLAB Compiler: 4.17 (R2012a)
  3 * Date: Mon Sep 09 16:19:01 2013
  4 * Arguments: "-B" "macro_default" "-W" "dotnet:PlotTest,TestDemo,0.0,private" "-T"
  5 * "link:lib" "-d" "D:\Work\DevelopMent_SVN\Matlab\MatlabBlog\PlotTest\src" "-w"
  6 * "enable:specified_file_mismatch" "-w" "enable:repeated_file" "-w"
  7 * "enable:switch_ignored" "-w" "enable:missing_lib_sentinel" "-w" "enable:demo_license"
  8 * "-v" "class{TestDemo:D:\Work\DevelopMent_SVN\Matlab\MatlabBlog\PlotTest.m}" 
  9 */
 10 using System;
 11 using System.Reflection;
 12 using System.IO;
 13 using MathWorks.MATLAB.NET.Arrays;
 14 using MathWorks.MATLAB.NET.Utility;
 15 
 16 #if SHARED
 17 [assembly: System.Reflection.AssemblyKeyFile(@"")]
 18 #endif
 19 
 20 namespace PlotTestNative
 21 {
 22 
 23   /// <summary>
 24   /// The TestDemo class provides a CLS compliant, Object (native) interface to the
 25   /// M-functions contained in the files:
 26   /// <newpara></newpara>
 27   /// D:\Work\DevelopMent_SVN\Matlab\MatlabBlog\PlotTest.m
 28   /// <newpara></newpara>
 29   /// deployprint.m
 30   /// <newpara></newpara>
 31   /// printdlg.m
 32   /// </summary>
 33   /// <remarks>
 34   /// @Version 0.0
 35   /// </remarks>
 36   public class TestDemo : IDisposable
 37   {
 38     #region Constructors
 39 
 40     /// <summary internal= "true">
 41     /// The static constructor instantiates and initializes the MATLAB Compiler Runtime
 42     /// instance.
 43     /// </summary>
 44     static TestDemo()
 45     {
 46       if (MWMCR.MCRAppInitialized)
 47       {
 48         Assembly assembly= Assembly.GetExecutingAssembly();
 49 
 50         string ctfFilePath= assembly.Location;
 51 
 52         int lastDelimiter= ctfFilePath.LastIndexOf(@"\");
 53 
 54         ctfFilePath= ctfFilePath.Remove(lastDelimiter, (ctfFilePath.Length - lastDelimiter));
 55 
 56         string ctfFileName = "PlotTest.ctf";
 57 
 58         Stream embeddedCtfStream = null;
 59 
 60         String[] resourceStrings = assembly.GetManifestResourceNames();
 61 
 62         foreach (String name in resourceStrings)
 63         {
 64           if (name.Contains(ctfFileName))
 65           {
 66             embeddedCtfStream = assembly.GetManifestResourceStream(name);
 67             break;
 68           }
 69         }
 70         mcr= new MWMCR("",
 71                        ctfFilePath, embeddedCtfStream, true);
 72       }
 73       else
 74       {
 75         throw new ApplicationException("MWArray assembly could not be initialized");
 76       }
 77     }
 78 
 79 
 80     /// <summary>
 81     /// Constructs a new instance of the TestDemo class.
 82     /// </summary>
 83     public TestDemo()
 84     {
 85     }
 86 
 87 
 88     #endregion Constructors
 89 
 90     #region Finalize
 91 
 92     /// <summary internal= "true">
 93     /// Class destructor called by the CLR garbage collector.
 94     /// </summary>
 95     ~TestDemo()
 96     {
 97       Dispose(false);
 98     }
 99 
100 
101     /// <summary>
102     /// Frees the native resources associated with this object
103     /// </summary>
104     public void Dispose()
105     {
106       Dispose(true);
107 
108       GC.SuppressFinalize(this);
109     }
110 
111 
112     /// <summary internal= "true">
113     /// Internal dispose function
114     /// </summary>
115     protected virtual void Dispose(bool disposing)
116     {
117       if (!disposed)
118       {
119         disposed= true;
120 
121         if (disposing)
122         {
123           // Free managed resources;
124         }
125 
126         // Free native resources
127       }
128     }
129 
130 
131     #endregion Finalize
132 
133     #region Methods
134 
135     /// <summary>
136     /// Provides a void output, 0-input Objectinterface to the PlotTest M-function.
137     /// </summary>
138     /// <remarks>
139     /// M-Documentation:
140     /// 編寫一個簡單的函式,對plot進行簡單封裝一下
141     /// </remarks>
142     ///
143     public void PlotTest()
144     {
145       mcr.EvaluateFunction(0, "PlotTest", new Object[]{});
146     }
147 
148 
149     /// <summary>
150     /// Provides a void output, 1-input Objectinterface to the PlotTest M-function.
151     /// </summary>
152     /// <remarks>
153     /// M-Documentation:
154     /// 編寫一個簡單的函式,對plot進行簡單封裝一下
155     /// </remarks>
156     /// <param name="n">Input argument #1</param>
157     ///
158     public void PlotTest(Object n)
159     {
160       mcr.EvaluateFunction(0, "PlotTest", n);
161     }
162 
163 
164     /// <summary>
165     /// Provides the standard 0-input Object interface to the PlotTest M-function.
166     /// </summary>
167     /// <remarks>
168     /// M-Documentation:
169     /// 編寫一個簡單的函式,對plot進行簡單封裝一下
170     /// </remarks>
171     /// <param name="numArgsOut">The number of output arguments to return.</param>
172     /// <returns>An Array of length "numArgsOut" containing the output
173     /// arguments.</returns>
174     ///
175     public Object[] PlotTest(int numArgsOut)
176     {
177       return mcr.EvaluateFunction(numArgsOut, "PlotTest", new Object[]{});
178     }
179 
180 
181     /// <summary>
182     /// Provides the standard 1-input Object interface to the PlotTest M-function.
183     /// </summary>
184     /// <remarks>
185     /// M-Documentation:
186     /// 編寫一個簡單的函式,對plot進行簡單封裝一下
187     /// </remarks>
188     /// <param name="numArgsOut">The number of output arguments to return.</param>
189     /// <param name="n">Input argument #1</param>
190     /// <returns>An Array of length "numArgsOut" containing the output
191     /// arguments.</returns>
192     ///
193     public Object[] PlotTest(int numArgsOut, Object n)
194     {
195       return mcr.EvaluateFunction(numArgsOut, "PlotTest", n);
196     }
197 
198 
199     /// <summary>
200     /// Provides an interface for the PlotTest function in which the input and output
201     /// arguments are specified as an array of Objects.
202     /// </summary>
203     /// <remarks>
204     /// This method will allocate and return by reference the output argument
205     /// array.<newpara></newpara>
206     /// M-Documentation:
207     /// 編寫一個簡單的函式,對plot進行簡單封裝一下
208     /// </remarks>
209     /// <param name="numArgsOut">The number of output arguments to return</param>
210     /// <param name= "argsOut">Array of Object output arguments</param>
211     /// <param name= "argsIn">Array of Object input arguments</param>
212     /// <param name= "varArgsIn">Array of Object representing variable input
213     /// arguments</param>
214     ///
215     [MATLABSignature("PlotTest", 1, 0, 0)]
216     protected void PlotTest(int numArgsOut, ref Object[] argsOut, Object[] argsIn, params Object[] varArgsIn)
217     {
218         mcr.EvaluateFunctionForTypeSafeCall("PlotTest", numArgsOut, ref argsOut, argsIn, varArgsIn);
219     }
220 
221     /// <summary>
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     /// </summary>
226     /// <remarks>
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.</remarks>
231     ///
232     public void WaitForFiguresToDie()
233     {
234       mcr.WaitForFiguresToDie();
235     }
236 
237 
238 
239     #endregion Methods
240 
241     #region Class Members
242 
243     private static MWMCR mcr= null;
244 
245     private bool disposed= false;
246 
247     #endregion Class Members
248   }
249 }
View Code

對比大家就可以發現,只不過一個更加傻瓜化,引數都是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: "-B" "macro_default" "-W" "dotnet:PlotTest,TestDemo,0.0,private" "-T"
 5 * "link:lib" "-d" "D:\Work\DevelopMent_SVN\Matlab\MatlabBlog\PlotTest\src" "-w"
 6 * "enable:specified_file_mismatch" "-w" "enable:repeated_file" "-w"
 7 * "enable:switch_ignored" "-w" "enable:missing_lib_sentinel" "-w" "enable:demo_license"
 8 * "-v" "class{TestDemo:D:\Work\DevelopMent_SVN\Matlab\MatlabBlog\PlotTest.m}" 
 9 */
10 using System;
11 using System.Reflection;
12 using System.IO;
13 using MathWorks.MATLAB.NET.Arrays;
14 using MathWorks.MATLAB.NET.Utility;

上面這段資訊主要是說明當前Matlab編譯器的版本,因為編譯的版本和部署的MCR版本必須對應起來,否則是不能執行的。然後有編譯日期,以及編譯的引數,類名以及M函式的地址等資訊,其實知道這些引數,理論上是可以自己在程式裡面呼叫Matlab的編譯器進行編譯工作的,只不過比較複雜,能力有限,研究不下去。下面的引用大家應該明白,這個是MWArray.dll裡面的名稱空間,所以混編的專案都要引用對應版本的MWArray.dll

2.關鍵的靜態建構函式

 1 static TestDemo()
 2     {
 3       if (MWMCR.MCRAppInitialized)
 4       {
 5         Assembly assembly= Assembly.GetExecutingAssembly();
 6         string ctfFilePath= assembly.Location;
 7         int lastDelimiter= ctfFilePath.LastIndexOf(@"\");
 8         ctfFilePath= ctfFilePath.Remove(lastDelimiter, (ctfFilePath.Length - lastDelimiter));
 9         string ctfFileName = "PlotTest.ctf";
10         Stream embeddedCtfStream = null;
11         String[] resourceStrings = assembly.GetManifestResourceNames();
12         foreach (String name in resourceStrings)
13         {
14           if (name.Contains(ctfFileName))
15           {
16             embeddedCtfStream = assembly.GetManifestResourceStream(name);
17             break;
18           }
19         }
20         mcr= new MWMCR("",ctfFilePath, embeddedCtfStream, true);
21       }
22       else
23       {
24         throw new ApplicationException("MWArray assembly could not be initialized");
25       }
26     }

  如果有一些C#的開發和程式設計經驗看上面的程式碼問題應該不大,否則還真有點難說清楚,簡單的說幾個要點:

  1)這個建構函式的作用主要是檢測MCR物件是否初始化,如果沒有,則尋找程式集,並拼接資源的位置,正確進行初始化

  2) 上面的ctf其實是包含在dll程式集裡面的,以資源的形式,這個檔案是核心,它才真正的包括了Matlab編譯之後的,MCR可以執行的中間程式。

  3) 必須要合法正確的ctf檔案和dll檔案才能 正確的初始化mcr物件,合法的意思,是Matlab內部有校驗機制,包括對相關版本,在以前的版本生成檔案中,很明顯,現在2012a裡面都隱藏起來了,不過要求都一樣。

  4) 上面方法其實已經間接的告訴了我們怎麼初始化mcr物件,有了mcr物件,一切都好辦了,因為它才是MCR的核心。 

3.PlotTest封裝的方法程式碼

 1     public void PlotTest()
 2     {
 3       mcr.EvaluateFunction(0, "PlotTest", new MWArray[]{});
 4     }
 5     public void PlotTest(MWArray n)
 6     {
 7       mcr.EvaluateFunction(0, "PlotTest", n);
 8     }
 9     public MWArray[] PlotTest(int numArgsOut)
10     {
11       return mcr.EvaluateFunction(numArgsOut, "PlotTest", new MWArray[]{});
12     }
13     public MWArray[] PlotTest(int numArgsOut, MWArray n)
14     {
15       return mcr.EvaluateFunction(numArgsOut, "PlotTest", n);
16     }

  看了欄位程式碼,再對應mcr的初始化,其實都很明朗了。通過mcr的EvaluateFunction來呼叫M函式。上面的程式碼有幾個過載方法,可以實用很多不同的情況,有時候,這些方法的個數會更多,其實沒多大必要,也可以自己編譯一下,把沒用的刪掉,保留少數幾個有用的即可。同時也可以看到,這裡直接通過字串來傳遞函式名稱的,因此必須保證這個函式能被mcr搜尋到。比如我們這裡的"PlotTest"這個函式其實就包含了ctf檔案中(注意ctf檔案是可以和dll分開的,在混編專案裡可以設定)。  

3.上述程式碼到內建函式的呼叫

   上述已經講解了整個mcr呼叫的過程,其實就是通過mcr的EvaluateFunction來呼叫M函式,但要保證對於的函式名稱在mcr搜尋的範圍內。那麼我們是不是可以假設:內建函式都在MCR內部,應該是可以搜尋到的,那麼把上面的函式名稱換一下,是不是也是可行的。這個假設也是我最早接觸時候的想法,有了假設,當然要去驗證。現在看來這個當然是肯定的,那麼不妨重新演示一遍。過程不詳細講了,程式碼也有註釋,混編要引用的MWArray.dll和名稱空間也不提了,看程式碼:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Reflection;
 6 using System.IO;
 7 
 8 using MathWorks.MATLAB.NET.Utility;
 9 using MathWorks.MATLAB.NET.Arrays;
10 
11 
12 namespace BuildInFunctionDemo
13 {
14     class Program
15     {
16         static MWMCR mcr;
17         static void Main(string[] args)
18         {
19             #region 首先使用PlotTest.dll來初始化mcr,因為這個dll是混編“合法”產生的,只有這樣才能順利啟動mcr
20             if (MWMCR.MCRAppInitialized)
21             {
22                 string path = Path.Combine(System.Environment.CurrentDirectory, "PlotTest.dll");
23                 Assembly assembly = Assembly.LoadFile(path);
24                 string ctfFilePath = assembly.Location;
25                 int lastDelimiter = ctfFilePath.LastIndexOf(@"\");
26                 ctfFilePath = ctfFilePath.Remove(lastDelimiter, (ctfFilePath.Length - lastDelimiter));
27                 string ctfFileName = "PlotTest.ctf";
28                 Stream embeddedCtfStream = null;
29                 String[] resourceStrings = assembly.GetManifestResourceNames();
30 
31                 foreach (String name in resourceStrings)
32                 {
33                     if (name.Contains(ctfFileName))
34                     {
35                         embeddedCtfStream = assembly.GetManifestResourceStream(name);
36                         break;
37                     }
38                 }
39                 mcr = new MWMCR("",ctfFilePath, embeddedCtfStream, true);
40             }
41             else
42             {
43                 throw new ApplicationException("MWArray assembly could not be initialized");
44             }
45             #endregion
46 
47             #region 直接呼叫混編dll中的封裝函式進行測試
48             mcr.EvaluateFunction(0, "PlotTest", 5);
49 
50             //注意這裡要斷點除錯才能看到效果哦,因為mcr會把圖繪製在一個Figure上面,
51             //後面的會覆蓋前面的,這裡暫停一下,可以看前面的效果
52             //下面就是直接呼叫matlab的plot函式的效果
53             MWNumericArray x = new double[]{1,2,3,4,5};
54             MWNumericArray y = new double[]{2,1,2.8,5.3,4.7};
55             mcr.EvaluateFunction(0, "plot",x,y );
56             #endregion
57 
58             Console.ReadKey();
59         }
60     }
61 }

唯一要注意的就是50-52的說明,要加斷點看2次繪製的效果。分別截圖如下:

4.總結

  拋磚引玉,這裡只是一個思路,附程式碼下載吧。

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

 

相關文章