日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++调用matlab编程

發布時間:2023/12/31 c/c++ 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++调用matlab编程 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

C++調用Matlab,實在是不得已的方法。原文鏈接:

http://kylen314.blog.com/2012/12/11/matlab_for_cplusplus/? 這是個很善良的博客,只出現了一個小小的錯誤!


內容:有少量修改!

噩耗啊噩耗啊!!!!最近本身就忙到吐,什么老板的專題研究,什么人臉性別識別,什么電功率分解算法研究,本身就快虛脫,然后昨天突然說一年半前申請的專利有個公式有點問題,我研究了半天也不知道一年半前的我為什么會寫出那樣的公式【囧】,然后昨天突然師兄跟我說,老板讓我這周內搭出某個系統的演示平臺,我勒個去,那個系統因為要從數字通信分析儀上獲取數據,所以用NI公司的GPIB采集卡,驅動文件里面提供了C++的接口,然后之后數據我是用matlab處理的,必須妥妥的嘛,結果現在要搭個系統意味著采集和處理要繼承在一起,我就慫了,我matlab里面調用了那么多計算方法不明的函數,要怎么移植到C++上啊,其實主要問題還是時間不夠,時間充足什么都好說。。。

所以只好飛快研究C++怎么調用matlab的函數了。。。研究了一下,方法好像很多,比如使用MATLAB Engine,它可以在C++里面調用直接使用matlab的函數,但是對于我這個系統,最好可以直接調用整個matlab的function,雖然MATLAB Engine我不知道可不可以,沒深究,后來研究了另外一個方法。總結一下當備忘。。


系統配置:

環境變量path中要加入matlab的bin目錄:即加上X:/XXXXXX/MATLAB/R2009a/bin/win32

雖然,我發現好像裝完matlab就已經自動加上了。。。

C++編譯器配置:

不管用VS也好,還是VC6.0也好,VC6中是打開Tool→Options→Directories,VS中是打開Tool→Options→Project and Solution→VC++ Directories,然后添加以下東西:

  • Include Files里面添加X:/XXXXXX/MATLAB/R2009a/extern/include
  • Include Files里面添加X:/XXXXXX/MATLAB/R2009a/extern/include/win32
  • Library Files里面添加X:/XXXXXX/MATLAB/R2009a/extern/lib/win32/microsoft
  • 這樣C++端就完成配置了。

    Matlab的配置:

  • 命令行中輸入mbuild -setup,第一個選y,第二個選你的編譯器,VS或者VC6.0對應的號碼,一般是2,第三個再選y
  • 命令行中輸入mex -setup,選項和第一步一樣。
  • 配置完上面的東西,就可以了~

    C++調用matlab使用方法:

  • 首先寫一個正確的matlab的m文件并保存,【額,不正確的我沒試過。。】比如說是functin [a b] = function_name(c,d)
  • matlab命令行中輸入deploytool
  • 在Deployment Tool左上角點擊Create a new deployment project
  • 選擇Matlab Compiler中的C++ Share Library
  • 并在下面輸入你的Project名字和路徑
  • 右鍵Add files加入你之前寫的m文件
  • 點擊Build the Project即可完成編譯。
  • 在你選擇的Project的路徑下有兩個文件夾,distrib和src,src那個不用管,打開distrib
  • 里面有三個文件是你要用的,.dll和.lib和.h文件
  • 建立你的C++工程,并將上一步中的三個文件加入到你的C++工程中即可。
  • C++代碼編寫:

    文件一開始要添加lib文件,代碼如下:

    #pragma comment(lib,"libdflapack.lib") #這個文件在2010a里面找不到,要去掉 #pragma comment(lib,"libemlrt.lib") #pragma comment(lib,"libeng.lib") #pragma comment(lib,"libfixedpoint.lib") #pragma comment(lib,"libmat.lib") #pragma comment(lib,"libmex.lib") #pragma comment(lib,"libmwlapack.lib") #pragma comment(lib,"libmwservices.lib") #pragma comment(lib,"libmx.lib") #pragma comment(lib,"libut.lib") #pragma comment(lib,"mclcommain.lib") #pragma comment(lib,"mclmcr.lib") #pragma comment(lib,"mclmcrrt.lib")

    注意一下倒數第三個,據說matlab后面的版本是mclcommain.lib,之前的版本是mclco.lib,沒考證,出了問題你去之前添加的lib路徑下看一下就知道了。

    然后添加你自己剛剛生成的lib文件

    #pragma comment(lib,"XXX.lib")

    之后必然要包含你自己的頭文件啦。。

    #include "XXX.h"

    至于怎么用那些函數,簡單說明一下,首先聲明一下,下文中提到的XXX都是你在上面第五步中寫的工程名,也就是上面lib和.h的XXX一樣。

    調用之前要先初始化,即調用函數:

    XXXInitialize();

    經過試驗,上面那個函數的運行之間極其之長。。。

    當然,在結束調用,或者在程序的尾部要調用:

    XXXTerminate();

    調用函數呢,就是你m文件的函數名,對了,眾所周知m文件的文件名要和m文件里面的函數名一樣,但是m文件的函數名不一定要和這里設置的Project一樣,比如說函數名叫myfunction(),而工程名叫test,那么你生成的就是test.h,test.dll,test.lib,但是你在C++代碼里面調用的時候用的卻是myfunction。

    如果你的myfunction沒有輸入輸出參數,那么調用的時候就直接寫myfunction()就可以了,如果有輸入輸出參數,函數調用格式是:

    myfunction(int nargout, mwArray& matrix...,const mwArray& n1....);

    直接理解就是輸出參數個數nargout

    然后nargout個輸出參數,數據類型是mwArray,稍有常識的人只要看到函數調用里面有&就基本是輸出參數啦~

    然后就是各個輸入參數,數據類型也是mwArray,看到const就知道應該是輸入參數啦~

    啥?你問我為什么不用寫輸入參數個數,好好復習matlab函數去!!!

    關于mwArray:

    這個是matlab生成的dll的接口數據類型,這個看上面的函數調用你就應該清楚這一點了,定義在matlab安裝目錄下的extern/include/下的mclcppclass.h下,有興趣的自己看去~

    雖然還有一種數據類型是mxArray,但是不推薦使用,而且mwArray本身就是對mxArray的封裝!不推薦使用是因為它的內存管理方式不好,而且使用的時候要用指針,而mwArray你可以直接作為對象來操作。【如果要生成接口為mxArray的dll的話,也可以在matlab命令行里面使用申明,但都說了。。不推薦。。】

    matlab里面不特別說明數據類型都是double,但是在這里定義一個mwArray數據的時候要說明類型,定義方法如下:

    mwArray matrix(2,2,mxDOUBLE_CLASS); mwArray matri2(1,m,mxINT8_CLASS);

    如果要賦值或者讀取,方法如下:

    mwArray matrix(1,4,mxDOUBLE_CLASS); double a[] = {1,2,3,4}; matrix.SetData(a,4);cout<<matrix(1,2)<<endl;double *b = new double[4]; matrix.GetData(b,4); cout<<b[0];

    可能出現的問題:

    C++調用matlab生成的lib的時候可能會出現runtime error R6034的錯誤,雖然你可以關掉這個錯誤,程序可以繼續跑,完全不礙事兒~但是也很煩,官方給出的說法是這樣子的:

    Summary

    On Windows (XP or Vista) error R6034 is thrown by C runtime.

    Description

    On Windows (XP or Vista) error R6034 is thrown by the C runtime. This happens when executing standalone applications, C or C++ user applications built using C or C++ shared libraries, MATLAB Builder JA components, and MATAB Builder NE components.

    In this case, the user has a PATH directory containing?msvcr80.dll. The error is thrown because the version of?tbbmalloc.dll?in?mcrinstallroot|matlabroot\bin\arch?does not contain a manifest.

    Workaround

    For standalone executables and C/C++ shared libraries using a supported Microsoft Visual Compiler:

  • Download the zip files in the attachment. New option files have been provided for both 32-bit and 64-bit Windows. These new options files create executables with manifests which the operating system uses to locate runtime shared libraries in the Windows side X side area. Applications created using the attached options files can be distributed to deployed machines without any change to the target MCR install area.
  • Back up?matlabroot\bin\arch\mbuildopts by executing the following at the system command prompt:?cd?matlabroot\bin\arch?& mkdir mbuildopts.bak & cd mbuildopts & copy *.*..\mbuildopts.bak?where?matlabroot?is the MATLAB installation directory which can be found by typing: matlabroot at the MATLAB command prompt and?arch?is the platform.
  • Unzip either?options_win32.zip?or?options_win64.zip?to the root of your MATLAB installation directory.
  • Execute?mbuild -setup.
  • Rebuild your application using the downloaded options files which you enabled by executingmbuild -setup.
  • For standalone executables?and C/C++ shared libraries Using Lcc-win32 compiler:

  • Create a manifest file named?application_name.exe.manifest?(if creating an executable) orapplication_name.dll.manifest?(if creating a shared library) with the following content: <?xml version='1.0' encoding='UTF-8' standalone='yes'?>? <assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>? <dependency>? <dependentAssembly>? <assemblyIdentity type='win32' name='Microsoft.VC80.CRT' version='8.0.50727.762' processorArchitecture='x86' publicKeyToken='1fc8b3b9a1e18e3b' />? </dependentAssembly>? </dependency>? </assembly>

    You can create this manifest file by copying the above content into a text file.

  • Place the manifest in the same folder as the executable. For example, if you have an application called?matrixdriver.exe, you would create a manifest file calledmatrixdriver.exe.manifest?and put it in the same folder as the application.
  • For MATLAB Builder for JA and MATLAB Builder for NE, perform the following on your development machine:

  • Change your folder to?matlabroot\bin\arch
  • Backup?tbbmalloc.dll?and rename it to?tbbmalloc.dll.bak
  • Execute the following command at a Visual Studio Command Prompt:?mt.exe -inputresource:libut.dll;#2 -outputresource:tbbmalloc.dll;#2
  • tbbmalloc.dll?now contains a manifest, and must be distributed to any machine on which you have encountered error R6034 when executing a MATLAB Builder JA or MATLAB Builder NE component. You?should place the modified?tbbmalloc.dll?in?mcrroot\bin\arch?where?mcrroot?is the MATLAB Compiler Runtime (MCR) installation directory.
  • Fix

    For R2009b, a new version of?tbbmalloc.dll?is distributed that contains a manifest.

    Attachments

    • options_win64.zip
    • options_win32.zip

    但是這個問題也不一定會發生,我在我電腦上跑的時候出現了這個問題,但是在另一臺新裝matlab的電腦上就沒出現,也許和網上傳言的一樣,重裝一下就可以了。。不過好麻煩的說。。。


    總結

    以上是生活随笔為你收集整理的C++调用matlab编程的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。