將matlab程式碼轉換成C程式碼
MATLAB Coder可以從MATLAB程式碼生成獨立的、可讀性強、可移植的C/C++程式碼。本文基於matlab2015a編制,但是也適用於matlab 2014~2016的版本。
1. 使用MATLAB Coder產生C程式碼的4個步驟:
(1) 開發實現特定演算法功能的MATLAB程式碼及其測試檔案;
(2) 檢查MATLAB程式碼的相容性,確保MATLAB程式碼中的語句都能被轉換成C程式碼(有些matlab程式碼語句並不能生成c/c++程式碼,例如matlab中的imread和imshow函式就不能被轉換成c程式碼);
(3) 產生最終使用的原始碼或MEX;
(4)
2. Matlab功能模組程式碼及其測試程式碼
2.1 foo.m
function c = foo(a, b)%#codegen
%This function muliplies a and b
c = a.* b;
2.2 foo_high.m
其中,%#codegen可以防止出現警告錯誤
function r1 = foo_high(v1, v2)%#codegen
%This function muliplies a and b
r1=zeros(1,3);
r1(1)=foo(v1(1),v2(1));
r1(2:3) = foo(v1(2:3),v2(2:3));
2.3 test_main.m
clear all
clc
close all
a=8.97;
b=1.786;
a=[2.1 7.3 11];
b=[1.9 3.3 8];
rc = foo_high(a, b)
2.4 results
rc =3.9900 24.0900 88.0000
3. matlab程式碼轉成C程式碼的詳細步驟
(1) 在命令視窗,輸入mex -setpu,選中一個存在的編譯器;
(2) 在命令視窗輸入coder(圖形介面),回車,彈出
或在matlab軟體的介面中點選MatLab Coder圖示。
生成的MATLAB Coder介面如下圖所示:
(3) 在Generate code for function框中輸入需要轉換為C程式碼的matlab功能模組檔案:foo.m和foo_high.m。
(4) 點選next按鈕,進入Define Input Types介面。輸入test_main.m測試檔案,
(5) 點選Autodefine Input Types按鈕,從而使得matlab能自動檢測出foo函式的介面變數的維度和型別。
(6) 點選next按鈕,進入check for run-time issues介面
點選check for issues按鈕,進入對應的介面:
上述問題表示被啟用的程式碼沒有呼叫foo函式的入口,即foo函式沒有被呼叫。因為foo函式的功能已經被包含在foo_high函式中了。這裡的紅色部分可以不用理會。
(7) 點選next按鈕,進入Generate Code介面:
點選“Generate”按鈕,matlab生成對應的C程式碼並顯示對應的介面:
(8) 點選next按鈕,顯示結束“Finish Workflow”介面:
4. 在vs2013中對轉換獲得的C程式碼進行測試。
Matlab在轉換生成的C程式碼後,會生成一個codegen資料夾,下面包含兩個子資料夾:lib和mex。Lib子資料夾下面包含xxx.h/xxx.c/xxx_initialize.c/xxx_initialize.c/xxx_tpyes.h
/rt_nonfinite.c/rt_nonfinite.h/rtGetInf.c/rtGetInf.h/rtGetNaN.c/rtGetNaN.h等檔案,其中只有xxx.c是轉換後所獲得的核心檔案。其它檔案都是支援性檔案。Mex資料夾下的檔案是具有冗餘的檔案,可不予考慮。
4.1 foo_high.c
void foo_high(const double v1[3],const double v2[3],double r1[3])
{
int i1;
/* This function muliplies a and b */
r1[0] = v1[0] * v2[0];
/* This function muliplies a and b */
for (i1 = 0; i1 < 2; i1++) {
r1[1 + i1] = v1[1 + i1] * v2[1 + i1];
}
}
4.2 Main.c
#include <iostream>
#include <D:\matlab_to_C\test_folder_5\vs_project2\foo_high.c>
using namespace std;
int main()
{
double a[3] = {4,9,2};
double b[3] = {2,7,8};
double c[3];
//cin >> a >> b;
foo_high(a,b,c);
cout << "c = " << c << endl;
cout << "c[0] = " << c[0] << endl;
cout << "c[1] = " << c[1] << endl;
cout << "c[2] = " << c[2] << endl;
cin.get();
return 0;
}
5. Example of C_Plus_Plus code
5.1 func1
#include <iostream>
using namespace std;
int func1(int a, int b)
{
int i0;
i0 = a*b;
cout << "\n***Good Test*******" << endl;
//return(int)i0;
return i0;
}
5.2 main
#include <stdio.h>
#include <iostream>
extern int func1(int a, int b);
//#include"func1.cpp"
using namespace std;
int main(void){
int i, a[10];
int b[8] = {11,22,33,44,55,66,77,88};
cout << "*****Good Test*******" << endl;
for (i = 0; i <= 9; i++)
a[i] = i;
for (i = 9; i >= 0; i--)
printf("%d ", a[i]);
for (i = 7; i >= 0; i--)
printf("%d ", b[i]);
int rt;
rt = func1(8, 9);
cout << "rt="<< rt << endl;;
cin.get();
return 0;
}
6. Ref: “C Code Generation Using the MATLAB Coder App” from the matlab website.
7. 備註
(1) 在Overview選項卡中,點選Add files,彈出對話方塊,選中foo.m開啟;
(2) 單擊變數a,選擇Define by Example…,彈出MATLAB Coder Define by Example對話方塊,在MATLAB Expression中輸入5,點選OK;同樣變數b也進行相應操作,輸入6;
(3) 選中Build選項卡,Output type中選擇c/c++ Static Library;選中Generate code only;
(4) 點選More settings,GeneralàLanguage選擇C++;Interface選項中去掉所有選項;Close;
(5) 點選Build,進行編譯;點選View report,彈出Code Generation Report對話方塊,此時,變數a、b、c會顯示相應的變數資訊;
(6) 利用vs2008建立一個控制檯應用程式,將生成的相關檔案foo.h、foo.cpp、rtwtypes.h、foo_types.h拷到相關目錄下並新增到應用程式中;
(7) 在foo.cpp檔案中新增#include “stdafx.h”;
(8) 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;}
7. 一個複雜的例子
求一個數的n次方根:
4.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 %after 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;
}
感謝 [南極粥](https://me.csdn.net/zyqdragon)