MATLABCoder可以从MATLAB代码生成独立的、可读性强、可移植的CC++代码。 使用MATLABCoder产生代码的3个步骤:准备用于产生代码的MATLAB算法;检查MATLAB代码的兼容性(有些matlab代码语句并不能生成cc++代码);产生最终使用的源代码或MEX。 利用MATLABCoder生成c++代码,并在vs2008中验证: 一个简单的例子,两数相乘: 1、安装matlab2011a或者更新版本; 2、简单生成一个foo.m文件; functionc=foo(a,b)%#codegen %Thisfunctionmulipliesaandb c=a*b 其中,%#codegen可以防止出现警告错误 3、在命令窗口,输入mex-setpu,选中一个存在的编译器; 4、在命令窗口输入coder(图形界面),回车,弹出MATLABCoderProject对话框; 5、在New选项卡Name中输入一个工程名foo.prj;点击Ok,弹出MATLABCoderMEXFunction对话框; 6、在Overview选项卡中,点击Addfiles,弹出对话框,选中foo.m打开; 7、单击变量a,选择DefinebyExample…,弹出MATLABCoderDefinebyExample对话框,在MATLABExpression中输入5,点击OK;同样变量b也进行相应操作,输入6; 8、选中Build选项卡,Outputtype中选择cc++StaticLibrary;选中Generatecodeonly; 9、点击Moresettings,GeneralàLanguage选择C++;Interface选项中去掉所有选项;Close; 10、点击Build,进行编译;点击Viewreport,弹出CodeGenerationReport对话框,此时,变量a、b、c会显示相应的变量信息; 11、利用vs2008建立一个控制台应用程序,将生成的相关文件foo.h、foo.cpp、rtwtypes.h、foo_types.h拷到相关目录下并添加到应用程序中; 12、在foo.cpp文件中添加#include“stdafx.h”; 13、test.cpp文件中代码为: #include"stdafx.h" #include"foo.h" #includeiostream
usingnamespacestd;
int_tmain(intargc,_TCHAR*argv[]) {
doublea=0.0,b=0.0,c=0.0;
cinab;
c=foo(a,b);
cout"c="cendl;
return0; } 一个复杂的例子,求一个数的n次方根: 1、两个.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~=3 tol=1e-9; else tol=varargin{3}; end
ifa0 nth_rt=0; iterations=0; hstry=0; else [nth_rt,hstry]=newtonSearchAlgorithm(a,n,tol); iterations=length(find(hstry~=0)); %iterations=sum(hstry~=0); end
newtonSearchAlgorithm.m: function[x,h]=newtonSearchAlgorithm(b,n,tol)%#codegen %Given,"a",thisfunctionfindsthenthrootofa %numberbyfindingwhere:x^n-a=0 coder.inline("never");%使其生成一个单独的c++文件 notDone=1; aNew=0;%RefinedGuessInitialization a=1;%InitialGuess cnt=0; h=zeros(50,1); h(1)=a; whilenotDone cnt=cnt+1; [curVal,slope]=f_and_df(a,b,n);%square yint=curVal-slope*a; aNew=-yintlope;%Thenewguess h(cnt)=aNew; if(abs(aNew-a)tol)%Breakifit"sconverged notDone=0; elseifcnt49%after50iterations,stop notDone=0; aNew=0; else a=aNew; end end x=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对话框; 3、在New选项卡Name中输入一个工程名nrt.prj;点击Ok,弹出MATLABCoderMEXFunction对话框; 4、在Overview选项卡中,点击Addfiles,弹出对话框,选中nrt.m打开; 5、添加三个输入,分别为10、2、1e-9;两个输入也可以; 6、选中Build选项卡,Outputtype中选择cc++StaticLibrary;选中Generatecodeonly; 7、点击Moresettings,GeneralàLanguage选择C++;Interface选项中去掉所有选项;Close; 8、点击Build,进行编译;点击Viewreport,弹出CodeGenerationReport对话框; 9、利用vs2008建立一个控制台应用程序,将生成的相关文件nrt.cpp、nrt.h、newtonSearchAlgorithm.cpp、newtonSearchAlgorithm.h、nrt_types.h、rtwtypes.h拷到相关目录下并添加到应用程序中; 10、分别在nrt.cpp、newtonSearchAlgorithm.cpp文件中添加#include“stdafx.h”; 11、test.cpp文件中代码为: #include"stdafx.h" #include"nrt.h"
#includeiostream
usingnamespacestd;
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;
cout"hstry_data="endl; for(inti=0;i50;i++) { couthstry_data[i]endl; }
cout"hstry_sizes="hstry_sizes[0]endl;
return0; }
信息发布:广州名易软件有限公司 http://www.myidp.net
|