|
MATLABCoder可以從MATLAB代碼生成獨(dú)立的、可讀性強(qiáng)、可移植的CC++代碼。 使用MATLABCoder產(chǎn)生代碼的3個(gè)步驟:準(zhǔn)備用于產(chǎn)生代碼的MATLAB算法;檢查MATLAB代碼的兼容性(有些matlab代碼語(yǔ)句并不能生成cc++代碼);產(chǎn)生最終使用的源代碼或MEX。 利用MATLABCoder生成c++代碼,并在vs2008中驗(yàn)證: 一個(gè)簡(jiǎn)單的例子,兩數(shù)相乘:1、安裝matlab2011a或者更新版本; 2、簡(jiǎn)單生成一個(gè)foo.m文件; functionc=foo(a,b)%#codegen%Thisfunctionmulipliesaandbc=a*b其中,%#codegen可以防止出現(xiàn)警告錯(cuò)誤3、在命令窗口,輸入mex-setpu,選中一個(gè)存在的編譯器; 4、在命令窗口輸入coder(圖形界面),回車,彈出MATLABCoderProject對(duì)話框; 5、在New選項(xiàng)卡Name中輸入一個(gè)工程名foo.prj;點(diǎn)擊Ok,彈出MATLABCoderMEXFunction對(duì)話框; 6、在Overview選項(xiàng)卡中,點(diǎn)擊Addfiles,彈出對(duì)話框,選中foo.m打開; 7、單擊變量a,選擇DefinebyExample…,彈出MATLABCoderDefinebyExample對(duì)話框,在MATLABExpression中輸入5,點(diǎn)擊OK;同樣變量b也進(jìn)行相應(yīng)操作,輸入6; 8、選中Build選項(xiàng)卡,Outputtype中選擇cc++StaticLibrary;選中Generatecodeonly; 9、點(diǎn)擊Moresettings,GeneralàLanguage選擇C++;Interface選項(xiàng)中去掉所有選項(xiàng);Close; 10、點(diǎn)擊Build,進(jìn)行編譯;點(diǎn)擊Viewreport,彈出CodeGenerationReport對(duì)話框,此時(shí),變量a、b、c會(huì)顯示相應(yīng)的變量信息; 11、利用vs2008建立一個(gè)控制臺(tái)應(yīng)用程序,將生成的相關(guān)文件foo.h、foo.cpp、rtwtypes.h、foo_types.h拷到相關(guān)目錄下并添加到應(yīng)用程序中; 12、在foo.cpp文件中添加#include“stdafx.h”; 13、test.cpp文件中代碼為: #include"stdafx.h"#include"foo.h"#includeiostreamusingnamespacestd;int_tmain(intargc,_TCHAR*argv[]){doublea=0.0,b=0.0,c=0.0;cinab;c=foo(a,b);cout"c="cendl;return0;} 一個(gè)復(fù)雜的例子,求一個(gè)數(shù)的n次方根:1、兩個(gè).m文件: nrt.m:function[nth_rt,iterations,hstry]=nrt(varargin)%#codegen%ThisfunctionwilluseaNewtonSearchTechniquetofind%thenthrootofanumber,a,tothetolerance,tol.%Thesquareroot%nrt(10,2),ornrt(10,2,1e-9)%The"n"root%nrt(10,n),ornrt(10,n,1e-9)a=varargin{1};n=varargin{2};ifnargin~=3tol=1e-9;elsetol=varargin{3};endifa0nth_rt=0;iterations=0;hstry=0;else[nth_rt,hstry]=newtonSearchAlgorithm(a,n,tol);iterations=length(find(hstry~=0));%iterations=sum(hstry~=0);endnewtonSearchAlgorithm.m:function[x,h]=newtonSearchAlgorithm(b,n,tol)%#codegen%Given,"a",thisfunctionfindsthenthrootofa%numberbyfindingwhere:x^n-a=0coder.inline("never");%使其生成一個(gè)單獨(dú)的c++文件notDone=1;aNew=0;%RefinedGuessInitializationa=1;%InitialGuesscnt=0;h=zeros(50,1);h(1)=a;whilenotDonecnt=cnt+1;[curVal,slope]=f_and_df(a,b,n);%squareyint=curVal-slope*a;aNew=-yintlope;%Thenewguessh(cnt)=aNew;if(abs(aNew-a)tol)%Breakifit"sconvergednotDone=0;elseifcnt49%after50iterations,stopnotDone=0;aNew=0;elsea=aNew;endendx=aNew;function[f,df]=f_and_df(a,b,n)%Ourfunctionisf=a^n-bandit"sderivativeisn*a^(n-1).f=a^n-b;df=n*a^(n-1);2、在命令窗口輸入coder(圖形界面),回車,彈出MATLABCoderProject對(duì)話框;3、在New選項(xiàng)卡Name中輸入一個(gè)工程名nrt.prj;點(diǎn)擊Ok,彈出MATLABCoderMEXFunction對(duì)話框; 4、在Overview選項(xiàng)卡中,點(diǎn)擊Addfiles,彈出對(duì)話框,選中nrt.m打開;5、添加三個(gè)輸入,分別為10、2、1e-9;兩個(gè)輸入也可以;6、選中Build選項(xiàng)卡,Outputtype中選擇cc++StaticLibrary;選中Generatecodeonly;7、點(diǎn)擊Moresettings,GeneralàLanguage選擇C++;Interface選項(xiàng)中去掉所有選項(xiàng);Close;8、點(diǎn)擊Build,進(jìn)行編譯;點(diǎn)擊Viewreport,彈出CodeGenerationReport對(duì)話框;9、利用vs2008建立一個(gè)控制臺(tái)應(yīng)用程序,將生成的相關(guān)文件nrt.cpp、nrt.h、newtonSearchAlgorithm.cpp、newtonSearchAlgorithm.h、nrt_types.h、rtwtypes.h拷到相關(guān)目錄下并添加到應(yīng)用程序中;10、分別在nrt.cpp、newtonSearchAlgorithm.cpp文件中添加#include“stdafx.h”;11、test.cpp文件中代碼為:#include"stdafx.h"#include"nrt.h"#includeiostreamusingnamespacestd;int_tmain(intargc,_TCHAR*argv[]){doublevarargin_1=0,varargin_2=0,varargin_3=1e-9;cinvarargin_1varargin_2;doublenth_rt=0,iterations=0;doublehstry_data[50]={0};inthstry_sizes[1]={0};nrt(varargin_1,varargin_2,varargin_3,nth_rt,iterations,hstry_data,hstry_sizes);cout"nth_rt="nth_rtendl;cout"iterations="iterationsendl;
|