監(jiān)理公司管理系統(tǒng) | 工程企業(yè)管理系統(tǒng) | OA系統(tǒng) | ERP系統(tǒng) | 造價咨詢管理系統(tǒng) | 工程設計管理系統(tǒng) | 簽約案例 | 購買價格 | 在線試用 | 手機APP | 產(chǎn)品資料
X 關閉

舉例說明使用MATLAB Coder從MATLAB生成C/C++代碼步驟

申請免費試用、咨詢電話:400-8352-114

  MATLAB Coder可以從MATLAB代碼生成獨立的、可讀性強、可移植的C/C++代碼。

使用MATLAB Coder產(chǎn)生代碼的3個步驟:準備用于產(chǎn)生代碼的MATLAB算法;檢查MATLAB代碼的兼容性(有些matlab代碼語句并不能生成c/c++代碼);產(chǎn)生最終使用的源代碼或MEX。

利用MATLAB Coder生成c++代碼,并在vs2008中驗證:

一個簡單的例子,兩數(shù)相乘

1、安裝matlab2011a或者更新版本;

2、簡單生成一個foo.m文件;

function c = foo(a, b)%#codegen

%This function muliplies a and b

c = a * b

其中,%#codegen可以防止出現(xiàn)警告錯誤

3、在命令窗口,輸入mex -setpu,選中一個存在的編譯器;

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”;

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;

}

 

一個復雜的例子,求一個數(shù)的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 %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;

}

 




發(fā)布:2007-04-16 17:22    編輯:泛普軟件 · xiaona    [打印此頁]    [關閉]

泛普入庫出庫管理軟件其他應用

藥品進銷存管理系統(tǒng) 醫(yī)藥進銷存 超市進銷存管理系統(tǒng) 服裝進銷存軟件 倉庫進銷存管理軟件 進銷存財務軟件 傻瓜進銷存 萬能進銷存軟件 進銷存網(wǎng)絡版 進銷存管理系統(tǒng) 進銷存系統(tǒng) 服裝庫存管理軟件 條碼倉庫管理軟件 庫存管理軟件 倉庫管理軟件 庫房管理軟件 出入庫管理軟件 倉儲管理系統(tǒng) 倉庫管理系統(tǒng) 庫存管理系統(tǒng) 入庫出庫管理軟件 進銷存軟件排名 倉庫管理軟件哪個好