小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

[求助]VC6.0調(diào)用MATLAB

 zqian84 2014-05-22
作  者: gaxlin
標(biāo) 題: VC++與MATLAB7.0以上版本連接方法
時(shí) 間: Sat Jan 27 21:23:05 2007
點(diǎn) 擊: 167

VC++ MATLAB 7 連接 混合編程 。本文適合VC++與MATLAB7.0以上版本連接方法,有些難,但
慢慢按這步驟一步步做是能看懂的,要有耐心,我研究了2天才實(shí)現(xiàn)了.如果有什么疑問可以
留言.
所有調(diào)用MATLAB7 Compiler產(chǎn)生的共享庫(kù)的程序都具有如下的大致結(jié)構(gòu):
1. 聲明變量或者是函數(shù)作為輸入變量;
2. 調(diào)用 mclInitalizeApplication函數(shù),并測(cè)試是否成功,該函數(shù)設(shè)置了一個(gè)全局的MC
R狀態(tài),并且構(gòu)建MCR實(shí)例;
3. 對(duì)于每個(gè)庫(kù),調(diào)用一次<libraryname>Initalize函數(shù),為庫(kù)創(chuàng)建一個(gè)MCR實(shí)例;
4. 調(diào)用庫(kù)中的函數(shù),并處理其結(jié)果(這是程序的主要部分);
5. 為每個(gè)庫(kù)調(diào)用一次<libraryname>Terminate函數(shù),用于注銷相聯(lián)系的MCR;
6. 調(diào)用mclTerminateApplication函數(shù),釋放與全局MCR狀態(tài)相聯(lián)系的資源;
7. 清除變換,關(guān)閉文件等,然后退出。
根據(jù)MATLAB的幫助文檔中提供的例子,利用如下文件進(jìn)行練習(xí):
<matlabroot>/extern/examples/compiler/addmatrix.m
<matlabroot>/extern/examples/compiler/multiplymatrix.m
<matlabroot>/extern/examples/compiler/eigmatrix.m
實(shí)現(xiàn)步驟:
1) 先將這幾個(gè)文件拷貝到當(dāng)前目錄下,然后利用mcc創(chuàng)建共享庫(kù),指令如下:
mcc -B csharedlib:libmatrix addmatrix.m multiplymatrix.m eigmatrix.m –v
其中,操作參數(shù) -B csharedlib 是一個(gè)綁定的操作,其等效指令為 -W lib:<libname> -
T link:lib。
2)在VC中創(chuàng)建一個(gè)MFC工程(本人創(chuàng)建的為基于對(duì)話框的),環(huán)境設(shè)置根據(jù)如下帖子:ht
tp://genial.yculblog.com/post.218721.html 指導(dǎo)進(jìn)行。在本例子中,只需要在VC中進(jìn)
行如下步驟:
A. ToolsàOptionsàDirectoriesàShow directories for:Include filesà<matlab7r
oot> \Extern\Include;
B. ToolsàOptionsàDirectoriesàShow directories for:Library filesà<matlab7r
oot> \Extern\Lib\Win32\Microsoft\msvc60;
C. ProjectàSettingàC/C++àCategory:Code GenerationàUse run-time library:D
ebug Multithread DLL;
D. ProjectàSettingàLinkàCategory:InputàObject/library modules:mclmcrrt.l
ib libmatrix.lib(mcc生成的共享庫(kù))。
3)拷貝MATLAB當(dāng)前目錄下剛才用mcc生成的libmatrix.h,libmatrix.dll,libmatrix.li
b,以及l(fā)ibmatrix.ctf文件到VC當(dāng)前工程目錄下,并用ProjectàAdd to ProjectàFiles
…將libmatrix.h加入到當(dāng)前工程中。
4)在當(dāng)前工程的對(duì)話框的頭文件中加入#include "libmatrix.h" 與 #include "mclmcr.
h";
5)在BOOL CMatlab7dllDlg::OnInitDialog()中進(jìn)行MATLAB庫(kù)文件的初始化,在void CMa
tlab7dllDlg::OnDestroy()中進(jìn)行MATLAB庫(kù)文件資源的釋放,否則可能出現(xiàn)按鈕只能夠按
一次,第二次運(yùn)行則出錯(cuò)的現(xiàn)象;
6)調(diào)用MATLAB產(chǎn)生的庫(kù)文件中函數(shù)的處理函數(shù)定義在一個(gè)按鈕的響應(yīng)函數(shù)中,并且要注意
的是:如果一個(gè)mxArray變量需要重用的時(shí)候,必須用mxDestroyArray(out); out=0;即先
進(jìn)行變量注銷,再設(shè)置為空。
附上這幾個(gè)主要函數(shù)如下:
1.BOOL CMatlab7dllDlg::OnInitDialog()
{
CDialog::OnInitDialog();
……………
// TODO: Add extra initialization here
/* Call the mclInitializeApplication routine. Make sure that the applicatio
n
* was initialized properly by checking the return status. This initializat
ion
* has to be done before calling any MATLAB API's or MATLAB Compiler genera
ted
* shared library functions. */
if( !mclInitializeApplication(NULL,0) )
{
AfxMessageBox( "Could not initialize the application.");
exit(1);
}
/* Call the library intialization routine and make sure that the
* library was initialized properly. */
if (!libmatrixInitialize())
{
AfxMessageBox("Could not initialize the library.");
exit(1);
}
return TRUE; // return TRUE unless you set the focus to a control
}

2.void CMatlab7dllDlg::OnDestroy()
{
CDialog::OnDestroy();
/* Call the library termination routine */
libmatrixTerminate();
mclTerminateApplication();
}

3.void CMatlab7dllDlg::OnRUN()
{
CString str;
mxArray *in1, *in2; /* Define input parameters */
mxArray *out = NULL;/* and output parameters to be passed to the library fu
nctions */

double data[] = {1,2,3,4,5,6,7,8,9};

/* Create the input data */
in1 = mxCreateDoubleMatrix(3,3,mxREAL);
in2 = mxCreateDoubleMatrix(3,3,mxREAL);

memcpy(mxGetPr(in1), data, 9*sizeof(double));
memcpy(mxGetPr(in2), data, 9*sizeof(double));

/* Call the library function */
mlfAddmatrix(1, &out, in1, in2);
/* Display the return value of the library function */
str="The value of added matrix is:\n";
str = str + Display(out);
AfxMessageBox(str);

/* Destroy the return value since this varaible will be resued in
* the next function call. Since we are going to reuse the variable,
* we have to set it to NULL. Refer to MATLAB Compiler documentation
* for more information on this. */
mxDestroyArray(out); out=0;

mlfMultiplymatrix(1, &out, in1, in2);
str = "The value of the multiplied matrix is:\n";
str = str+Display(out);
AfxMessageBox(str);

mxDestroyArray(out); out=0;

mlfEigmatrix(1, &out, in1);
str = "The Eigen value of the first matrix is:\n";
str = str+Display(out);
AfxMessageBox(str);

mxDestroyArray(out); out=0;
/* Free the memory created */
mxDestroyArray(in1); in1=0;
mxDestroyArray(in2); in2 = 0;

AfxMessageBox("OK, Finished!");
}

4.CString CMatlab7dllDlg::Display(const mxArray *in)
{
CString str, strout=" ";
int i=0, j=0; /* loop index variables */
int r=0, c=0; /* variables to store the row and column length of the matrix
*/
double *data; /* variable to point to the double data stored within the mxA
rray */
/* Get the size of the matrix */
r = mxGetM(in);
c = mxGetN(in);
/* Get a pointer to the double data in mxArray */
data = mxGetPr(in);
/* Loop through the data and display the same in matrix format */
for( i = 0; i < c; i++ ){
for( j = 0; j < r; j++){
str.Format("%4.2f\t",data[i*c+j]);
strout = strout+str;
}
strout = strout+"\n";
}
strout = strout +"\n";
return strout;
}

5.附m文件:
1)addmatrix.m
function a = addmatrix(a1, a2)
%ADDMATRIX Add two matrices
% Copyright 2003 The MathWorks, Inc.
a = a1 + a2;

2) multiplymatrix.m
function m = multiplymatrix(a1, a2)
%MULTIPLYMATRIX Multiplies two matrices
% Copyright 2003 The MathWorks, Inc.
m = a1*a2;

3) eigmatrix.m
function e = eigmatrix(a1)
%EIGMATRIX Returns the eigen value of the given matrix
% Copyright 2003 The MathWorks, Inc.
e = eig(a1);
發(fā)布方法:
在MATLAB7.0的TOOL目錄下找到MCRInstaller.exe,在目標(biāo)機(jī)器上點(diǎn)擊運(yùn)行MCRInstaller.e
xe文件,安裝好MCR之后,將<mcr_root>\runtime\win32加入到系統(tǒng)的環(huán)境變量path中去。

同時(shí)還要copy工程文件的可執(zhí)行程序,共享庫(kù)(DLL),共享庫(kù)對(duì)應(yīng)的.ctf文件,在VC++源程
序的DEBUG目錄下。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多