举例说明使⽤MATLABCoder从MATLAB⽣成CC++代码步骤 MATLAB Coder可以从MATLAB代码⽣成独⽴的、可读性强、可移植的C/C++代码。
使⽤MATLAB Coder产⽣代码的3个步骤:准备⽤于产⽣代码的MATLAB算法;检查MATLAB代码的兼容性(有些matlab代码语句并不能⽣成c/c++代码);产⽣最终使⽤的源代码或MEX。
利⽤MATLAB Coder⽣成c++代码,并在vs2008中验证:西方政治制度
⼀个简单的例⼦,两数相乘:
1、安装matlab2011a或者更新版本;
2、简单⽣成⼀个foo.m⽂件;
function c = foo(a, b)%#codegen
%This function muliplies a and b
c = a * b
其中,%#codegen可以防⽌出现警告错误
3、在命令窗⼝,输⼊mex -tpu,选中⼀个存在的编译器;
荣耀平板24、在命令窗⼝输⼊coder(图形界⾯),回车,弹出MATLAB Coder Project对话框;
5、在New选项卡Name中输⼊⼀个⼯程名foo.prj;点击Ok,弹出MATLAB Coder MEX Function对话框;
6、在Overview选项卡中,点击Add files,弹出对话框,选中foo.m打开;
7、单击变量a,选择Define by Example…,弹出MATLAB Coder Define by Example对话框,在MATLAB Expression中输⼊5,点击OK;同样变量b也进⾏相应操作,输⼊6;
8、选中Build选项卡,Output type中选择c/c++ Static Library;选中Generate code only;
9、点击More ttings,GeneralàLanguage选择C++;Interface选项中去掉所有选项;Clo;
10、点击Build,进⾏编译;点击View report,弹出Code Generation Report对话框,此时,变量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"
歌手用英语怎么说
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
double a = 0.0, b = 0.0, c = 0.0;
cin>>a>>b;
c = foo(a, b);
cout<<"c = "<<c<<endl;
return 0;
}
⼀个复杂的例⼦,求⼀个数的n次⽅根:
1、 两个.m⽂件:
nrt.m:
function [nth_rt, iterations, hstry] = nrt(varargin)%#codegen %This function will u a Newton Search Technique to find %the nth root of a number, a, to the tolerance, tol.
李健吉他谱%The square root
% nrt(10, 2), or nrt(10, 2, 1e-9)
%The "n" root
淞沪抗战纪念馆%nrt(10, n), or nrt(10, n, 1e-9)
a = varargin{1};
n = varargin{2};
if nargin ~= 3
tol = 1e-9;
el
tol = varargin{3};
end
if a < 0
nth_rt = 0;
iterations = 0;
hstry = 0;
[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", this function finds the nth root of a
%number by finding where: x^n-a = 0
coder.inline('never'); %使其⽣成⼀个单独的c++⽂件
notDone = 1;
aNew = 0; %Refined Guess Initialization
a = 1; %Initial Guess
关于新年的儿歌cnt = 0;
h = zeros(50, 1);
h(1) = a;
while notDone
cnt = cnt + 1;
[curVal, slope] = f_and_df(a, b, n); % square
yint = curVal - slope * a;
aNew = -yint / slope; %The new guess
h(cnt) = aNew;
if (abs(aNew-a) < tol) %Break if it's converged
notDone = 0;
elif cnt > 49 %after 50 iterations, stop
notDone = 0;
aNew = 0;
el
a = aNew;
end
我的世界tpend
x = aNew;
function [f, df] = f_and_df(a, b, n)
%Our function is f=a^n-b and it's derivative is n*a^(n-1).
f = a^n-b;
三年级语文上册课本df = n*a^(n-1);
2、 在命令窗⼝输⼊coder(图形界⾯),回车,弹出MATLAB Coder Project对话框;
3、在New选项卡Name中输⼊⼀个⼯程名nrt.prj;点击Ok,弹出MATLAB Coder MEX Function对话框;
4、在Overview选项卡中,点击Add files,弹出对话框,选中nrt.m打开;
5、添加三个输⼊,分别为10、2、1e-9;两个输⼊也可以;
6、选中Build选项卡,Output type中选择c/c++ Static Library;选中Generate code only;
7、点击More ttings,General-->Language选择C++;Interface选项中去掉所有选项;Clo;
8、点击Build,进⾏编译;点击View report,弹出Code Generation Report对话框;
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"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
double varargin_1 = 0, varargin_2 = 0, varargin_3 = 1e-9;
cin>>varargin_1>>varargin_2;
double nth_rt = 0, iterations = 0;
double hstry_data[50] = {0};
int hstry_sizes[1] = {0};
nrt(varargin_1, varargin_2, varargin_3, &nth_rt, &iterations, hstry_data, hstry_sizes);
cout<<"nth_rt = "<<nth_rt<<endl;
cout<<"iterations = "<<iterations<<endl;
cout<<"hstry_data = "<<endl;
for (int i=0; i<50; i++)
{
cout<<hstry_data[i]<<endl;
}
cout<<"hstry_sizes = "<<hstry_sizes[0]<<endl;
return 0;
}
参考:
1、录制的⽹上研讨会的MATLAB Coder视频