一、問題描述

  因為Matlab這個工具十分的強大
  但有些函式並不是我們可以輕易的重寫出來使用
  所以需透過其它方法先把檔案轉成DLL檔
  來讓C#測試使用

二、方法
  主要是先參考以下網站所分享的方式
  http://larry-why.blogspot.tw/2013/08/matlab-dll-c.html
  但
  事情並非那樣的單純
  我遇到不同的錯誤訊息~
  最後是透過其它設定的模式來解決的~
  以下會順道分享我的解決方式

  環境:Matlab R2009b 32 bits
     Visual Studio 2010 C# 32 bits
     Windows 7 64bits

  step1:在Matlab 的 Command Windows 下,輸入 deploytool 。
      並輸入專案名稱
      請注意,Target 需選擇.NET Assembly
      matlab_dll 
       matlab_dll
      
      在右方的Deployment Tool
      新增類別名稱、並加入您想要最後轉成的dll 的matlab檔案
      (package內也需加入此matlab檔案) 
       matlab_dll
       matlab_dll  
      matlab_dll  

      建置
      matlab_dll

      matlab_dll  
      matlab_dll  
     
  step2:開啟 Visual Studio,並且建立一個 C# Console 應用程式
      加入剛剛建置好的.DLL 檔。(路徑位於專案資料夾裡面)
      亦需加入Matlab型態變數陣列的.DLL檔案喔~
       C:\Program Files\MATLAB\R2009b\toolbox\dotnetbuilder\bin\win64\v2.0\MWArray

      matlab_dll  

      matlab_dll
      matlab_dll
      
    ERROR!!!與解決方式(1) 處理器架構不符
      錯誤清單

      描述
        警告 1 建置 "AMD64" 之專案的處理器架構與參考 "MWArray, Version=2.9.1.0, Culture=neutral, PublicKeyToken=e1d84a0da19db86f, processorArchitecture=x86" 的處理器架構 "x86" 不符。這可能會導致執行階段失敗。請考慮透過 [組態管理員] 變更專案的目標處理器架構,使專案與參考之間的處理器架構對應,或者使用符合專案目標處理器架構之處理器架構的參考相依性。 ConsoleApplication1

      matlab_dll
      matlab_dll    

      最後在C#的表頭加入即可使用
      using twice;
      using MathWorks.MATLAB.NET.Arrays;
      using MathWorks.MATLAB.NET.Utility;

     ERROR!!!與解決方式(2) 資料型態不符
      undefined function or method 'eig' for input arguments of type 'int32'

      可嘗試將資料型態更改一下試看看~
      double[,] a1 = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; //Matrix 1
      double[,] b1 = { { 3, 3, 3 }, { 7, 8, 9 }, { 4, 5, 6 } }; //Matrix 2
      MWNumericArray arr1 = a1;
      MWNumericArray arr2 = b1;
      MWNumericArray result = (MWNumericArray)abc.test(arr1, arr2);

      ERROR!!!與解決方式(3) "混合模式組件是針對版本 v2.0.50727 的執行階段建置的,無法在沒有其他組態資訊的情況下載入 4.0 執行階段中"
      在專案裡的app.config裡做更改即可
       <?xml version="1.0" encoding="utf-8" ?>
       <configuration>
        <startup useLegacyV2RuntimeActivationPolicy="true">
         <supportedRuntime version="v4.0"/>
        </startup>
       </configuration>

      matlab _ error
      matlab_error
      matlab_error  

         
      若您無此app.config檔案,請自行新增

      matlab_error  

    註:矩陣模式供C#使用
      MWNumericArray result_hp = (MWNumericArray)matlab_fun.eegfilt_fun(arr_data_buff, sampleRate, mi_freq[k * 2], 0);
      //double[,] hp_arr = (double[,])((MWNumericArray)result_hp).ToArray(MWArrayComponent.Real);
      MWNumericArray result_fbEEGdata = (MWNumericArray)matlab_fun.eegfilt_fun(result_hp, sampleRate, 0, mi_freq[k * 2 + 1]);
      double[,] fbEEGdata = (double[,])((MWNumericArray)result_fbEEGdata).ToArray(MWArrayComponent.Real);

      ※ 如果想在沒安裝 MATLAB 的電腦上執行,需安裝 MATLAB Compiler Runtime (MCR),檔案在安裝目錄的 toolbox\compiler\deploy\win32\MCRInstaller.exe,大小約 150MB

arrow
arrow

    HuangJung1216 發表在 痞客邦 留言(0) 人氣()