當前位置:工程項目OA系統(tǒng) > 領域應用 > 倉庫進銷存管理系統(tǒng) > 入庫出庫管理軟件
舉例說明使用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;
}
- 1施工管理軟件
- 2網(wǎng)店管理軟件
- 3服務行業(yè)美容院管理軟件
- 4印刷管理軟件
- 5出入庫管理軟件
- 6預算管理軟件
- 7員工管理軟件
- 8工作日程管理軟件
- 9商務管理軟件
- 10企業(yè)管理軟件
- 11目標管理軟件
- 12運輸管理軟件
- 1無法升級更新
- 2如何使用Tasklist命令
- 3IP地址沖突問題的解決方法
- 4電腦死機故障分析
- 5更改IE的默認搜索引擎
- 6內部網(wǎng)上的IP地址應該如何設置
- 7解開被鎖定的.reg與.inf文件
- 8工廠倉庫管理軟件(網(wǎng)絡版)
- 9如何徹底刪除輸入法文件
- 10WinXP瘦身辦法
- 11工廠倉庫管理軟件 3.02
- 12設置任務管理器
- 13C++為什么難學
- 14改良程序需要的一些技巧11個
- 15操作系統(tǒng)進程描述
- 16泛普入庫出庫管理軟件軟件/免費醫(yī)藥進銷存系統(tǒng)/店鋪商品出入庫打單使用技巧總結
- 17Regsvr32 用法和錯誤消息的說明
- 18網(wǎng)卡沒有安裝成功該怎么解決
- 19用SQL語句,熟練使用刪除語句
- 20隱藏桌面‘回收站’的兩個方法
- 21雙系統(tǒng)的安全卸載
- 22破解網(wǎng)頁禁用鼠標右鍵
- 23解決鼠標右鍵被鎖定,我的電腦不知道為什么鼠標右鍵被鎖定了,用不了。請問如何解決?
- 24入庫出庫管理軟件智能、高效、操作便利
- 25BIOS與CMOS區(qū)別
- 26部分軟件無法安裝
- 27系統(tǒng)無法自動保存設置
- 28如何知道局域網(wǎng)中所有計算機的IP地址
- 29如何重新安裝IE瀏覽器
- 30入庫出庫管理軟件免費版 5.1.0
成都公司:成都市成華區(qū)建設南路160號1層9號
重慶公司:重慶市江北區(qū)紅旗河溝華創(chuàng)商務大廈18樓