?? matlab與vc集成.htm
字號:
<P class=a14><B>Matlab Add-in</B>
<P class=a14>Matlab
6.0以后版本對其Compiler作了較大的改進,其支持更多的數據類型,更強的優化功能,更為重要的是其Matlab
Add-in提供了一個Matlab和VC直接集成的途徑。它具有一些新的特征:快速集成M文件到C++工程中,創建獨立的 C/C++應用程序或C MEX
DLL;通過M文件創建共享庫或MEX文件;內含Visual Matrix Viewer,調試過程中可以查看矩陣變量的值;
直接修改M源文件而不是修改生成的C/C++文件;方便快捷地打包應用程序等。
<P class=a14>Matlab Add-in的安裝:
<P class=a14>1.
啟動Matlab,運行“mex-setup”,按照菜單提示選取MSVC。它將配置MEX使用MSVC為默認的編譯器(創建C-MEX文件必需),并安裝Matlab
Add-in所需文件到MSVC目錄。
<P class=a14>2. 在Matlab環境下運行“mbuild-setup”,
按提示選取MSVC。它將配置使用MSVC為默認的編譯器(創建獨立應用程序必需),并安裝Matlab Add-in所需的Matlab
Compiler和C/C++數學庫文件到MSVC目錄。
<P class=a14>3.
在Matlab環境下運行“cd(prefdir)”和“mccsavepath”。它將保存當前Matlab路徑到mccpath文件中,這是因為Matlab
Add-in脫離Matlab運行,否則它無從知道Matlab路徑。
<P class=a14>4. 在MSVC環境中配置Matlab Add-in,方法是:從菜單“Tools ->
Customize”選擇“Add-ins and Macro Files”選項卡,選中“Matlab Add-in”,
點擊“Close”。MSVC工具欄出現Matlab Add-in圖標,以后每次啟動VC,自動加載Matlab Add-in。
<P class=a14>5. 配置Windows系統。在Windows 95/98系統下,找到config.sys, 添加一行:shell=
c:\ command.com /e:32768 /p(Windows
ME有所不同,需設置Conagent.exe的屬性Memory選項卡的Initial
Environment值為4096)。從表面看,VC的環境設置也是步驟較多,但只需設置一次。
<P class=a14><B>程序示例</B>
<P
class=a14>下面以創建一個Standalone程序為例具體說明。啟動MSVC,打開“File->New”菜單,選擇Project,選擇“Matlab
Project Wizard”, Project Name命名為Mytest, 點擊Next,你可以選擇所生成的應用程序的類型:Windows
Console EXE、C-MEX DLL、M-MEX DLL、Shared M-DLL;
C程序還是C++程序;選取Debug、Release模式;選擇是否生成Main函數;是否需要支持圖形。這里選擇生成C++程序,其余采取默認設置,點擊“Finish”,出現對話框要求選擇添加到工程中的M源文件,選擇文件testplot.m后,這時你可以看到MSVC調用MCC生成一個混合M文件和C的工程。在“File
View”選項卡中可以看到添加的M文件和自動生成的C++文件。
<P class=a14>這時大功告成,即可按F7編譯后按F5運行了,運行結果如下圖所示。
<P><IMG align=right src="Matlab與VC集成.files/c05_2t1.jpg">
<P class=a14><B>程序運行結果</B>
<P class=a14>這里用到的testplot.m內容如下(先在Matlab里編輯好):
<P class=a14>function mytest()
<P class=a14>t=0: 0.05*pi: 2*pi;
<P class=a14>y=sin(t);
<P class=a14>plot(t,y,‘b*’);
<P class=a14>可以通過雙擊Matlab
M-files文件夾下testplot.m打開觀察。生成的testplot.cpp代碼如下(略有刪節):
<P class=a14>// MATLAB Compiler: 2.1 Date: Sat Nov 03 16:37:08 2001
<P class=a14>#include “testplot.hpp”
<P class=a14>#include “libmatlbm.hpp”
<P class=a14>static double _array0_[41] = { 0.0,.157,....};
<P class=a14>//自動生成的數組
<P class=a14>void InitializeModule_testplot() {
<P class=a14>}
<P class=a14>void TerminateModule_testplot() {
<P class=a14>}
<P class=a14>static void Mtestplot();
<P class=a14>_mexLocalFunctionTable _local_function_
<P class=a14>table_testplot = { 0, (mexFunctionTableEntry *)NULL };
<P class=a14>void mlxTestplot(int nlhs, mxArray * plhs[], int nrhs,
mxArray * prhs[]) {
<P class=a14>MW_BEGIN_MLX();
<P class=a14>{
<P class=a14>if (nlhs > 0) {
<P class=a14>error(mwArray(“Run-time Error: File: testplot Line: 1 Column:
1 The function \‘testplot\’was called with more than the declared number
of outputs (0). ”));
<P class=a14>}
<P class=a14>if (nrhs > 0) {
<P class=a14>error(mwArray(“Run-time Error: File: testplot Line: 1 Column:
1 The function \‘testplot\’ was called with more than the declared number
of inputs (0).”));
<P class=a14>}
<P class=a14>Mtestplot();
<P class=a14>}
<P class=a14>MW_END_MLX();
<P class=a14>}
<P class=a14>#line 1 “D:\\ygbtest\\testplot.m”
<P class=a14>static void Mtestplot() {
<P class=a14>#line 1 “D:\\ygbtest\\testplot.m”
<P class=a14>mclMlineEnterFunction(“D:\\ygbtest\\testplot
<P class=a14>.m”, “testplot”)
<P class=a14>#line 1 “D:\\ygbtest\\testplot.m”
<P class=a14>mwLocalFunctionTable save_local_
<P class=a14>function_table_(&_local_function_table_testplot);
<P class=a14>#line 1 “D:\\ygbtest\\testplot.m”
<P class=a14>mwArray ans(mclGetUninitializedArray());
<P class=a14>#line 1 “D:\\ygbtest\\testplot.m”
<P class=a14>mwArray y(mclGetUninitializedArray());
<P class=a14>#line 1 “D:\\ygbtest\\testplot.m”
<P class=a14>mwArray t(mclGetUninitializedArray());
<P class=a14>// t=0: 0.05*pi: 2*pi;
<P class=a14>//
<P class=a14>#line 2 “D:\\ygbtest\\testplot.m”
<P class=a14>mclMline(2);
<P class=a14>#line 2 “D:\\ygbtest\\testplot.m”
<P class=a14>t = mwArray(1, 41, _array0_, (double *)NULL);
<P class=a14>//
<P class=a14>// y=sin(t);
<P class=a14>//
<P class=a14>#line 3 “D:\\ygbtest\\testplot.m”
<P class=a14>mclMline(3);
<P class=a14>#line 3 “D:\\ygbtest\\testplot.m”
<P class=a14>y = sin(mwVv(t,“t”));
<P class=a14>//
<P class=a14>// plot(t,y,‘b*’);
<P class=a14>//
<P class=a14>#line 4 “D:\\ygbtest\\testplot.m”
<P class=a14>mclMline(4);
<P class=a14>#line 4 “D:\\ygbtest\\testplot.m”
<P class=a14>ans.EqAns(Nplot(0, mwVarargin(mwVv(t, “t”), mwVv(y, “y”),
“b*”)));
<P class=a14>#line 4 “D:\\ygbtest\\testplot.m”
<P class=a14>mclMlineExitFunction();
<P class=a14>#line 4 “D:\\ygbtest\\testplot.m”
<P class=a14>}
<P class=a14>自動生成的testplot_mainhg.cpp代碼如下:
<P class=a14>#include “matlab.hpp”
<P class=a14>//加入Matlab頭文件
<P class=a14>#include “libsglinit.hpp”
<P class=a14>#include “testplot.hpp”
<P class=a14>//加入應用程序頭文件
<P class=a14>static mexFunctionTableEntry function_table[1] = { {
“testplot”, mlxTestplot, 0, 0, &_local_function_table_testplot } };
<P class=a14>static _mexcppInitTermTableEntry init_term_table[1] = { {
InitializeModule_testplot, TerminateModule_testplot } };
<P class=a14>static _mexcpp_information _main_info = { 1, 1,
function_table, 0, NULL, 0, NULL, 1, init_term_table };
<P
class=a14>可以看到沒有添加一句C/C++代碼,即生成了一個Matlab的Standalone應用程序(是不是太容易啦)。需要注意兩點:
一是添加到MSVC工程的文件應當為一個函數文件,而不能是一個Script文件,否則會出現錯誤提示;
二是最好不要修改生成的C/C++代碼,如果需要改動,可以通過在MSVC工作區中直接修改M文件(無須在Matlab環境中),重新編譯即可。你可以通過在M文件中設置斷點,在運行中通過Matrix
Viewer觀察Matlab變量的值。
<P
class=a14>目前通過Add-in實現Matlab與VC的混合編程也有一定的不足:如目前并不支持所有的Matlab的函數;生成的程序代碼有些繁雜等。估計Matlab
6.0以后的版本在這方面肯定有所改進的。
<P class=a14><B>結 論</B>
<P class=a14>Matlab
Add-in提供了一種實現Matlab和VC混合編程的直接集成的途徑,通過它可以很方便地生成Matlab和VC的混合程序。在圖像處理等需要大量計算的工程應用場合有廣泛的應用價值。
<P align=right>(計算機世界報 第51期 C15、C16) </P></TD></TR></TBODY></TABLE>
<TABLE align=center width=680>
<TBODY>
<TR>
<TD>
<HR color=#f46240 SIZE=1 width=660>
</TD></TR></TBODY></TABLE><BR>
<P align=center>Copyright(C) ccw.com.cn,All rights reserved</P>
<P align=center><B>中國計算機世界出版服務公司版權所有</B></P></CENTER></BODY></HTML>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -