MatLab API in C优秀课件

上传人:夏** 文档编号:588529790 上传时间:2024-09-08 格式:PPT 页数:21 大小:135KB
返回 下载 相关 举报
MatLab API in C优秀课件_第1页
第1页 / 共21页
MatLab API in C优秀课件_第2页
第2页 / 共21页
MatLab API in C优秀课件_第3页
第3页 / 共21页
MatLab API in C优秀课件_第4页
第4页 / 共21页
MatLab API in C优秀课件_第5页
第5页 / 共21页
点击查看更多>>
资源描述

《MatLab API in C优秀课件》由会员分享,可在线阅读,更多相关《MatLab API in C优秀课件(21页珍藏版)》请在金锄头文库上搜索。

1、MatLabAPIinC+ChristopherDabney1 1Purposen nMatLabn nisaninterpretedscriptinglanguageisaninterpretedscriptinglanguagen nconversiontoobjectcodeisatruntime;conversiontoobjectcodeisatruntime;computationtimecanbealittleslowcomputationtimecanbealittleslown nhasexcellentprototypingandplottinghasexcellentpr

2、ototypingandplottingfunctionalityfunctionalityn ncontainsconvenientandveryrobustmatrixcontainsconvenientandveryrobustmatrixoperationpackagesoperationpackages2 2Purpose(cont)n nC+n nisaprogramminglanguage,optimalandwithisaprogramminglanguage,optimalandwithhighspeedfloatingpointcomputationhighspeedflo

3、atingpointcomputationn nisnon-trivialtoproducevisualeffectsinisnon-trivialtoproducevisualeffectsinn neg.plotting,GUIs,nicetableseg.plotting,GUIs,nicetablesn ncanbedifficulttosecurearobustvectorcanbedifficulttosecurearobustvectoralgebrapackageforalgebrapackagefor3 3Purpose(cont)n nSolution:C+invoking

4、MatLabcommandsn nnumbercrunchinginC+numbercrunchinginC+n nmatrixoperationsinMatLabmatrixoperationsinMatLabn nplotting,graphing,tablesinMatLabplotting,graphing,tablesinMatLabn nForprogrammerswitharobust&complexC+programintendingtoplotresultstheyarealreadyobtainingn nForMatLabprogrammingwithscriptswhi

5、chfrequentlylag4 4InteractingwithMatLabn nNotrestrictedtoC+;Java,Perl,Fortran,andNotrestrictedtoC+;Java,Perl,Fortran,andotherlanguagescandoitalsootherlanguagescandoitalson nNotrestrictedtotheMSVisualStudioNotrestrictedtotheMSVisualStudioenvironmentthedemosarewritteninenvironmentthedemosarewritteninn

6、 nAlsopossibletoinvokeaC+routinefromAlsopossibletoinvokeaC+routinefromMatLabsenvironmentMatLabsenvironmentn nThreewaystointeractThreewaystointeractn nSendDatafromC+toMatLabSendDatafromC+toMatLabn nCallaMatLabfunctionfromC+CallaMatLabfunctionfromC+n nGenerateaDynamicLinkLibrary(dll)filefroma.mfileGen

7、erateaDynamicLinkLibrary(dll)filefroma.mfilen nWewillusethefirstoneWewillusethefirstone5 5DemoRequirementsn nAC+Compilern nthedemousesMSVisualC+v.6.0thedemousesMSVisualC+v.6.0n nMatLabn nthedemousesMatLabv.7.0.1thedemousesMatLabv.7.0.1n nAssumespriorprogrammingexperienceinbothMatLabandC+n nnoexplana

8、tionsaregivenforthecommandsnoexplanationsaregivenforthecommandsn nFollowingslidesexplainsettingupthedemonstrationprogramenvironment6 6SettinguptheEnvironmentn nCreatingpathstoMatLab:-Tools-Options-Tools-Options-DirectoriesTab-DirectoriesTab-SelectIncludefilesfromthedrop-downlist-SelectIncludefilesfr

9、omthedrop-downlist-Add-AddC:MATLAB701EXTERNINCLUDEC:MATLAB701EXTERNINCLUDEtothelisttothelist-SelectLibraryFilesfromthedrop-downlist-SelectLibraryFilesfromthedrop-downlist-Add-Add”C:MATLAB701EXTERNLIBWIN32MICROSOFTMSVC60“”C:MATLAB701EXTERNLIBWIN32MICROSOFTMSVC60“n nLibrarypathsvarydependingonthelangu

10、ageandLibrarypathsvarydependingonthelanguageandcompilercompiler7 7Screenshot8 8SettinguptheEnvironment(cont)n nTwowaystoincludetheappropriatelibraryfiles:Twowaystoincludetheappropriatelibraryfiles:Method1)Method1)-Project-Settings-Project-Settings-LinksTab-LinksTabUnderObject/LibraryModule:,addthree

11、filenames:UnderObject/LibraryModule:,addthreefilenames:-Libmx.lib-Libmx.lib-libmex.lib-libmex.lib-libeng.lib-libeng.libMethod2)Method2)AddthefollowinglinesofcodejustundertheAddthefollowinglinesofcodejustundertheincludesinthesourcecode:includesinthesourcecode: #pragma comment( lib, Libmx.lib )#pragma

12、 comment( lib, Libmx.lib ) #pragma comment( lib, libmex.lib )#pragma comment( lib, libmex.lib ) #pragma comment( lib, libeng.lib )#pragma comment( lib, libeng.lib )9 9Screenshot1010ConnectingtoMatLabEnginen nHeader:Header:#include#include,MatLabsEngine,MatLabsEnginen nIntheprogram,createapointertoit

13、:Intheprogram,createapointertoit:Engine *m_pEngine;Engine *m_pEngine;m_pEngine = engOpen(NULL);m_pEngine = engOpen(NULL);if (m_pEngine = NULL)if (m_pEngine = NULL) cout Error: Not Found” cout Error: Not Found” endl; endl; exit(1); exit(1); 1111InvokingCommandsn nengEvalString(Engine*ptr,stringcmd);e

14、ngEvalString(m_pEngine, “x = 1:1:10);“ );engEvalString(m_pEngine, “x = 1:1:10);“ );engEvalString(m_pEngine, “y = x.2;“ );engEvalString(m_pEngine, “y = x.2;“ );engEvalString(m_pEngine, “plot(x,y);“ );engEvalString(m_pEngine, “plot(x,y);“ );n nEnablesprogrammertoinvokeanycommandinMatLabfromtheC+platfo

15、rm(convenient)n nNotspecialbyitself-mightaswellworkstraightfromMatLab1212IObetweenC+andMatLabn nUsethe“matrixarray”mxArraydatatypen nEveryvariableinMatLabisamatrixforC+toexchangedatawithit,adatatypethatbothC+andMatLabrecognizeisneededn nmxArraycan“bundle”C+variablesasmatrices,sobothplatformsrecogniz

16、ethisdatatype1313InsertingValues(input)n nTopassavariable,eg.x0,intoMatLab,createanTopassavariable,eg.x0,intoMatLab,createanmxArraymxArrayforitforit-Allocatethespace(-Allocatethespace(mxCreateDoubleMatrixmxCreateDoubleMatrix) )-Copythevalue(-Copythevalue(memcpymemcpy) )-NamethevariableintheEngine(-N

17、amethevariableintheEngine(engPutVariableengPutVariable) )double x0;double x0;mxArray *m_X;mxArray *m_X;m_X=mxCreateDoubleMatrix(1, 1, mxREAL);m_X=mxCreateDoubleMatrix(1, 1, mxREAL);memcpy(void *)mxGetPr(m_X), (void *)x, memcpy(void *)mxGetPr(m_X), (void *)x, sizeof(double)*1);sizeof(double)*1);engPu

18、tVariable(m_pEngine, x, m_X);engPutVariable(m_pEngine, x, m_X);n nPointerPointerm_pEnginem_pEngineisusedtospecifytheengineisusedtospecifytheenginen nVariablexintheMatLabEnginegetsthevalueofx0inVariablexintheMatLabEnginegetsthevalueofx0inC+C+1414ExtractingValues(output)n nToextracttheMatLabEnginesvar

19、iablez,pullitintoToextracttheMatLabEnginesvariablez,pullitintoC+asaC+asamxArraymxArraymatrix,thenextractthebundledmatrix,thenextractthebundledvalueoutofthatvalueoutofthatmxArraymxArrayandintoaC+variableandintoaC+variabledouble *cresult;double *cresult;mxArray *mresult;mxArray *mresult;mresult = engG

20、etVariable(m_pEngine,z);mresult = engGetVariable(m_pEngine,z);cresult = mxGetPr(mresult);cresult = mxGetPr(mresult);cout cresult0;cout cresult0;n nmxGetPrmxGetPrreturnsapointertoacopyofthereturnsapointertoacopyofthedoubledoublevaluevaluezstoredintheMatLabEnginezstoredintheMatLabEnginen nWarning:Unpr

21、edictablefatalerrorsoccurifthedatatypeWarning:UnpredictablefatalerrorsoccurifthedatatypeinMatLabdoesntcloselyresemblethedatatypeinC+inMatLabdoesntcloselyresemblethedatatypeinC+eg.Copyinga3x1vectorintoascalareg.Copyinga3x1vectorintoascalar1515PassingArrays&Matricesn nTopassarrays(vector),adjustthesiz

22、eparametersofTopassarrays(vector),adjustthesizeparametersofthememoryallocationandcopyroutinetomatchthethememoryallocationandcopyroutinetomatchthedimensionsofthearraybeingpasseddimensionsofthearraybeingpassedn nPassavectoraofdimensions:1xSIZEPassavectoraofdimensions:1xSIZEdouble adouble aSIZESIZE;mxA

23、rray *A;mxArray *A;/ assume a gets initialized, all values/ assume a gets initialized, all valuesA=mxCreateDoubleMatrix(1, A=mxCreateDoubleMatrix(1, SIZESIZE, mxREAL);, mxREAL);memcpy(void *)mxGetPr(A), (void *)a, memcpy(void *)mxGetPr(A), (void *)a, sizeof(double)*sizeof(double)*SIZESIZE););engPutV

24、ariable(m_pEngine, a, A);engPutVariable(m_pEngine, a, A);n nCopiesentireC+arrayaintotheMatLabEnginesaCopiesentireC+arrayaintotheMatLabEnginesa1616PassingArrays&Matrices(cont)n nPassamatrixofdimensions:SIZExSIZEPassamatrixofdimensions:SIZExSIZEdouble cdouble cSIZESIZESIZESIZE;/ assume c gets initiali

25、zed, all of it / assume c gets initialized, all of it mxArray *mxc;mxArray *mxc;mxc = mxCreateDoubleMatrix(mxc = mxCreateDoubleMatrix(SIZESIZE, , SIZESIZE, mxREAL);, mxREAL);memcpy(void *) mxGetPr(mxc), (void *)c, memcpy(void *) mxGetPr(mxc), (void *)c, sizeof(double)*sizeof(double)*SIZE*SIZESIZE*SI

26、ZE););engPutVariable(m_pEngine, c, mxc);engPutVariable(m_pEngine, c, mxc);engEvalString(m_pEngine, c = c;);engEvalString(m_pEngine, c = c;);n nNote:C+hasrow-majorstorage,andMatLabhasNote:C+hasrow-majorstorage,andMatLabhascolumn-majorstorage.Thusamatrixbeingexchangedcolumn-majorstorage.Thusamatrixbei

27、ngexchangedmustbetransposed,eitherbeforeoraftertheexchange,mustbetransposed,eitherbeforeoraftertheexchange,tomaintainthematrixssemanticstomaintainthematrixssemantics1717SeetheDemosProgramn nDemonstratesallofthesefeaturessofarinMicrosoftVisualDevelopmentStudioC+v.6.0n nDemonstratesgraphplots,invoking

28、commands,input,output,andpassingvectorsandmatrices.1818InternetReferencesn nWelcometoZhenwangsHomepage(Q&A)WelcometoZhenwangsHomepage(Q&A)n nhttp:/www.sfu.ca/zyao/teaching/ensc488faq.htmhttp:/www.sfu.ca/zyao/teaching/ensc488faq.htmn nATutorialtoCallMATLABFunctionsfromWithinAATutorialtoCallMATLABFunc

29、tionsfromWithinAC/C+ProgramC/C+Programn nhttp:/prism.mem.drexel.edu/Shah/public_html/c2matlab.htmhttp:/prism.mem.drexel.edu/Shah/public_html/c2matlab.htmn nMicrosoftVisualStudioMicrosoftVisualStudioC+C+&InterfacingMatlabwith&InterfacingMatlabwithC/C+,JavaC/C+,Javan nhttp:/www.qcf.gatech.edu/academic

30、/LabDataAccess/C+IntroductionMatLab.Inhttp:/www.qcf.gatech.edu/academic/LabDataAccess/C+IntroductionMatLab.Interfacings.docterfacings.docn nMatLabTheLanguageofTechnicalComputing,MatLabTheLanguageofTechnicalComputing,ExternalInterfacesExternalInterfacesn nhttp:/.au/access/helpdesk/help/pdf_doc/matlab/apiext.pdfhttp:/.au/access/helpdesk/help/pdf_doc/matlab/apiext.pdf1919Questions?2020ThankYouNoMatLabmatriceswereharmedinthemakingofthispresentation.2121

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 商业/管理/HR > 市场营销

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号