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 -setup,選中一個存在的編譯器;
4、在命令窗口輸入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 settings,GeneralàLanguage選擇C++;Interface選項中去掉所有選項;Close;
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”;//VC++6.0找不到這個庫,實際測試時,忽略
13、test.cpp文件中代碼爲: //自行編寫的一個測試文件
//#include "stdafx.h" //忽略
#include "foo.h"
#include <iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[]) //實際測試時改爲 main()
{
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 use 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;
else
tol = varargin{3};
end
if a < 0
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", 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;
elseif cnt > 49 �ter 50 iterations, stop
notDone = 0;
aNew = 0;
else
a = aNew;
end
end
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 settings,GeneralàLanguage選擇C++;Interface選項中去掉所有選項;Close;
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;
}