您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 质量控制/管理 > matlab转C语言
GeneratingCCodeUsingMATLABCoder版本:2011a改到没有错误为止。DesignConsiderationsWhenWritingMATLABCodeforCodeGenerationWhenwritingMATLABcodethatyouwanttoconvertintoefficient,standaloneC/C++code,youmustconsiderthefollowing:DatatypesCandC++usestatictyping.Todeterminethetypesofyourvariablesbeforeuse,MATLABCoderrequiresacompleteassignmenttoeachvariable.ArraysizingVariable-sizearraysandmatricesaresupportedforcodegeneration.Youcandefineinputs,outputs,andlocalvariablesinMATLABfunctionstorepresentdatathatvariesinsizeatruntime.MemoryYoucanchoosewhetherthegeneratedcodeusesstaticordynamicmemoryallocation.Withdynamicmemoryallocation,youpotentiallyuselessmemoryattheexpenseoftimetomanagethememory.Withstaticmemory,yougetbestspeedperformance,butwithhighermemoryusage.MostMATLABcodetakesadvantageofthedynamicsizingfeaturesinMATLAB,thereforedynamicmemoryallocationtypicallyenablesyoutogeneratecodefromexistingMATLABcodewithoutmodifyingitmuch.Dynamicmemoryallocationalsoallowssomeprogramstocompilesuccessfullyevenwhenupperboundscannotbefound.Staticallocationreducesthememoryfootprintofthegeneratedcode,andthereforeissuitableforapplicationswherethereisalimitedamountofavailablememory,suchasembeddedapplications.SpeedBecauseembeddedapplicationsmustruninrealtime,thecodemustbefastenoughtomeettherequiredclockrate.Toimprovethespeedofthegeneratedcode:oChooseasuitableC/C++compiler.ThedefaultcompilerthatMathWorkssupplieswithMATLABforWindows32-bitplatformsisnotagoodcompilerforperformance.SettingUpYourCCompilerTosetuptheinstalledcompiler:1.AttheMATLABcommandline,enter:mex-setup2.Enterytoseethelistofinstalledcompilers.3.Selectasupportedcompiler.4.Enterytoverifyyourchoice.5.Considerdisablingrun-timechecks.codeanalyzerTousethecodeanalyzertoidentifywarningsanderrorsspecifictoMATLABforcodegeneration,youmustaddthe%#codegendirective(orpragma)toyourMATLABfile.YouusethecodeanalyzerintheMATLABEditortocheckforcodeviolationsatdesigntime,minimizingcompilationerrors.IntheCurrentFolderbrowsertoolbar,clicktheActionsbutton,andthenselectReportsCodeAnalyzerReport.HowtoDisableRun-TimeChecksBydefault,forsafety,thecodegeneratedforyourMATLABcodecontainsmemoryintegritychecksandresponsivenesschecks.Generally,thesechecksresultinmoregeneratedcodeandslowersimulation.Disablingrun-timechecksusuallyresultsinstreamlinedgeneratedcodeandfastersimulation.Disablethesechecksonlyifyouaresurethatyourcodeissafeandthatarrayboundsanddimensioncheckingisunnecessary.Youcandisablerun-timechecksexplicitlyfromtheprojectsettingsdialogbox,thecommandline,oraMEXconfigurationdialogbox.DisablingRun-TimeChecksintheProjectSettingsDialogBox1.OntheMATLABCoderprojectBuildtab,clickMoresettings.TheProjectSettingsdialogboxopens.2.OntheGeneraltab,clearEnsurememoryintegrity,Ensureresponsiveness,orExtrinsiccalls,asapplicable.DisablingRun-TimeChecksFromtheCommandLine1.IntheMATLABworkspace,definetheMEXconfigurationobject:mexcfg=coder.config('mex');2.Atthecommandline,settheIntegrityChecks,ExtrinsicCalls,orResponsivenessCheckspropertiestofalse,asapplicable:3.mexcfg.IntegrityChecks=false;4.mexcfg.ExtrinsicCalls=false;mexcfg.ResponsivenessChecks=false;DisablingRun-TimeChecksintheAutomaticCMEXGenerationDialogBox1.IntheMATLABworkspace,definetheMEXconfigurationobject:mexcfg=coder.config('mex');2.OpentheAutomaticCMEXGenerationdialogbox:openmexcfg3.OnthedialogboxGeneraltab,clearEnsurememoryintegrity,Ensureresponsiveness,orExtrinsiccalls,asapplicable,andclickApply.SettingUpaMATLABCoderProject1.FromtheMATLABmainmenu,selectFileNewCodeGenerationProject.2.TheMATLABCoderProjectdialogboxopens.3.Inthisdialogbox,enteranamefortheproject,forexample,kalman_filter,andclickOK.Overviewtab,clickAddfiles.TheAddFilesdialogboxopens.kalman01.mandclickOpen.MATLABCoderaddsthefiletotheproject.kalman01functionhasoneinputparameter,z,whichappearsbelowthefilename.Notethatitstypeisundefined.allvariablesintheMATLABfilesatcompiletime.Therefore,youmustspecifythepropertiesofallfunctioninputsatthesametimeasyoucompilethefile.Tocompilekalman01.m,youmustspecifythesizeoftheinputvectorz.Here,youdefinezbyexample,usingdatastoredinthefileposition.matthatyouwanttheKalmanfilteralgorithmtoprocess.a.Loadtheposition.matfileintoyourMATLABworkspace.loadposition.matThiscommandloadsamatrixpositioncontainingthexandycoordinatesof310pointsinCartesianspace.b.IntheMATLABCoderproject,ontheOverviewtab,selecttheinputparameterzandclicktheActionsicontotherightofztoopenthecontextmenu.c.Fromthecontextmenu,selectDefinebyExample.TheDefinebyExampledialogboxopens.d.IntheMATLABExpressionfield,enterposition(1:2,1)andthenclickOK.MATLABCoderusestheclass,size,andcomplexityofthisexpressiontodefinez.GeneratingaMEXFunctionUsingMATLABCoderBeforegeneratingCcode,itisbestpracticetogenerateaMEXfunctionthatyoucanexecutewithintheMATLABenvironmenttotestyouralgorithmandcheckforrun-timeerrors.ClicktheMATLABCoderprojectBuildtab.Bydefault,theprojectwillgenerateaMEXfunctionnamedkalman01_mex.Thefileextensionisplatformdependent.ValidatingtheMEXFunctionRunningtheGeneratedMEXFunctiontestscriptincludethefollowingsent
本文标题:matlab转C语言
链接地址:https://www.777doc.com/doc-5610833 .html