使用MATLAB Engine与C混合编程Word下载.docx
《使用MATLAB Engine与C混合编程Word下载.docx》由会员分享,可在线阅读,更多相关《使用MATLAB Engine与C混合编程Word下载.docx(19页珍藏版)》请在冰豆网上搜索。
/*
*engdemo.c
*Thisisasimpleprogramthatillustrateshowtocallthe
*MATLABenginefunctionsfromaCprogram.
*/
#include
#include"
engine.h"
#defineBUFSIZE256
intmain()
{
Engine*ep;
mxArray*T=NULL,*result=NULL;
charbuffer[BUFSIZE];
doubletime[10]={0.0,1.0,2.0,3.0,4.0,5.0,6.0,7.0,
8.0,9.0};
6-6
/*
*StarttheMATLABenginelocallybyexecutingthestring
*"
matlab"
.
*Tostartthesessiononaremotehost,usethenameof
*thehostasthestringratherthan\0
*Formorecomplicatedcases,useanystringwithwhitespace,
*andthatstringwillbeexecutedliterallytostartMATLAB.
*/
if(!
(ep=engOpen("
\0"
))){
fprintf(stderr,"
\nCan'
tstartMATLABengine\n"
);
returnEXIT_FAILURE;
}/*启动MATLAB引擎*/
*PARTI
*Forthefirsthalfofthisdemonstration,wewillsenddata
*toMATLAB,analyzethedata,andplottheresult.
*Createavariableforourdata.
T=mxCreateDoubleMatrix(1,10,mxREAL);
/*创建一个矩阵*/
mxSetName(T,"
T"
/*设置矩阵的名字为“T”*/
memcpy((void*)mxGetPr(T),(void*)time,sizeof(time));
/*向矩阵“T”赋值*/
*把矩阵“T”置入MATLAB引擎
engPutArray(ep,T)
*Evaluateafunctionoftime,distance=(1/2)g.*t.^2
*(gistheaccelerationduetogravity).
engEvalString(ep,"
D=.5.*(–9.8).*T.^2;
"
/*执行MATLAB命令:
D=.5.*(–9.8).*T.^2;
*绘制图象.
plot(T,D);
/*执行MATLAB命令:
绘图*/
title('
Positionvs.Timeforafalling
object'
给图象加标题*/
xlabel('
Time(seconds)'
设置X轴坐标*/
ylabel('
Position(meters)'
设置Y轴
坐标*/
*Usefgetc()tomakesurethatwepauselongenoughtobe
*abletoseetheplot.
printf("
Hitreturntocontinue\n\n"
fgetc(stdin);
*We'
redoneforPartI!
Freememory,closeMATLABengine.
DoneforPartI.\n"
mxDestroyArray(T);
/*从内存中撤销矩阵“T”*/
close;
/*关闭刚才显示图象的窗口*/
*PARTII
*Forthesecondhalfofthisdemonstration,wewillrequest
*aMATLABstring,whichshoulddefineavariableX.MATLAB
*willevaluatethestringandcreatethevariable.We
*willthenrecoverthevariable,anddetermineitstype.
*/
*UseengOutputBuffertocaptureMATLABoutput,sowecan
*echoitback.
engOutputBuffer(ep,buffer,BUFSIZE);
/*构建MATLAB文本输入缓冲区*/
while(result==NULL){
charstr[BUFSIZE];
*Getastringinputfromtheuser.
EnteraMATLABcommandtoevaluate.This
commandshould\n"
createavariableX.Thisprogramwillthen
determine\n"
whatkindofvariableyoucreated.\n"
Forexample:
X=1:
5\n"
>
"
/*要求用户输入一个MATLAB命令*/
fgets(str,BUFSIZE–1,stdin);
/*获得用户输入*/
*EvaluateinputwithengEvalString.
engEvalString(ep,str);
/*执行用户输入的MATLAB命令*/
*Echotheoutputfromthecommand.Firsttwocharacters
*arealwaysthedoubleprompt(>
).
%s"
buffer+2);
/*显示该MATLAB命令的执行情况*/
*Getresultofcomputation.
\nRetrievingX...\n"
if((result=engGetArray(ep,"
X"
))==NULL)
/*判断是否可以从MATLAB引擎中获得矩阵“X”*/
Oops!
Youdidn'
tcreateavariableX.\n\n"
else
Xisclass%s\t\n"
mxGetClassName(result));
/*显示矩阵“X”的类型*/
}/*while(result==NULL)*/
redone!
Freememory,closeMATLABengineandexit.
Done!
\n"
mxDestroyArray(result);
engClose(ep);
/*关闭MATLAB引擎*/
returnEXIT_SUCCESS;
/*返回*/
}
4、引擎应用程序的编译
对于象上例中的控制台程序,可以在MATLAB命令行中直接使用带-f参数的mex命令编译。
如果在普通win32application中使用MATLAB引擎,情况则比较复杂。
在Windows中,MATLAB引擎是通过ActiveX被调用的。
因此你需要先Create一个OLEAutomationSever和一个OLEClient,然后通过OLE方式调用这个MATLAB引擎。
具体做法可参阅相关MATLAB随机文档。
5、总结
MATLAB引擎的调用与其它引擎(例如数据库引擎)的调用很类似,其步骤是联接\启动引擎,然后向引擎发送命令,获得引擎处理结果。
结束语
上面简要介绍了MATLAB与C++的几种接口方式,我们可以根据要求的不同采用相应的方式。
此外,MATLAB还提供了一个数学库,由此数学库,我们可以获得对MATLAB内部命令更多的访问权和更灵活的访问方式。
具体内容可参考MATLAB的相关随机文档。
•参考文献
1、MATLAB随机文档:
apiguide.pdf
2、MATLAB随机文档:
apiref.pdf
3、MATLAB随机文档:
c_math_ref1.pdf
VC程序员最好用visualmatcom。
VB用matrixVBmathtool在主页上提供免费试用,快去下吧。
matlab的功能可在你的VC,VB中实现,而且只需两