<output id="r87xx"></output>
    1. 
      
      <mark id="r87xx"><thead id="r87xx"><input id="r87xx"></input></thead></mark>
        •   

               當(dāng)前位置:首頁(yè)>管理咨詢>舉例說明使用MATLAB Coder從MATLAB生成C/C++代碼步驟 查詢:
               
          舉例說明使用MATLAB Coder從MATLAB生成C/C++代碼步驟
          MATLABCoder可以從MATLAB代碼生成獨(dú)立的、可讀性強(qiáng)、可移植的CC++代碼。

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

          利用MATLABCoder生成c++代碼,并在vs2008中驗(yàn)證:

          一個(gè)簡(jiǎn)單的例子,兩數(shù)相乘

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

          2、簡(jiǎn)單生成一個(gè)foo.m文件;

          functionc=foo(a,b)%#codegen

          %Thisfunctionmulipliesaandb

          c=a*b

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

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

          4、在命令窗口輸入coder(圖形界面),回車,彈出MATLABCoderProject對(duì)話框;

          5、在New選項(xiàng)卡Name中輸入一個(gè)工程名foo.prj;點(diǎn)擊Ok,彈出MATLABCoderMEXFunction對(duì)話框;

          6、在Overview選項(xiàng)卡中,點(diǎn)擊Addfiles,彈出對(duì)話框,選中foo.m打開;

          7、單擊變量a,選擇DefinebyExample…,彈出MATLABCoderDefinebyExample對(duì)話框,在MATLABExpression中輸入5,點(diǎn)擊OK;同樣變量b也進(jìn)行相應(yīng)操作,輸入6;

          8、選中Build選項(xiàng)卡,Outputtype中選擇cc++StaticLibrary;選中Generatecodeonly;

          9、點(diǎn)擊Moresettings,GeneralàLanguage選擇C++;Interface選項(xiàng)中去掉所有選項(xiàng);Close;

          10、點(diǎn)擊Build,進(jìn)行編譯;點(diǎn)擊Viewreport,彈出CodeGenerationReport對(duì)話框,此時(shí),變量a、b、c會(huì)顯示相應(yīng)的變量信息;

          11、利用vs2008建立一個(gè)控制臺(tái)應(yīng)用程序,將生成的相關(guān)文件foo.h、foo.cpp、rtwtypes.h、foo_types.h拷到相關(guān)目錄下并添加到應(yīng)用程序中;

          12、在foo.cpp文件中添加#include“stdafx.h”;

          13、test.cpp文件中代碼為:

          #include"stdafx.h"

          #include"foo.h"

          #includeiostream

          usingnamespacestd;

          int_tmain(intargc,_TCHAR*argv[])

          {

          doublea=0.0,b=0.0,c=0.0;

          cinab;

          c=foo(a,b);

          cout"c="cendl;

          return0;

          }

          一個(gè)復(fù)雜的例子,求一個(gè)數(shù)的n次方根

          1、兩個(gè).m文件:

          nrt.m:

          function[nth_rt,iterations,hstry]=nrt(varargin)%#codegen

          %ThisfunctionwilluseaNewtonSearchTechniquetofind

          %thenthrootofanumber,a,tothetolerance,tol.

          %Thesquareroot

          %nrt(10,2),ornrt(10,2,1e-9)

          %The"n"root

          %nrt(10,n),ornrt(10,n,1e-9)

          a=varargin{1};

          n=varargin{2};

          ifnargin~=3

          tol=1e-9;

          else

          tol=varargin{3};

          end

          ifa0

          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",thisfunctionfindsthenthrootofa

          %numberbyfindingwhere:x^n-a=0

          coder.inline("never");%使其生成一個(gè)單獨(dú)的c++文件

          notDone=1;

          aNew=0;%RefinedGuessInitialization

          a=1;%InitialGuess

          cnt=0;

          h=zeros(50,1);

          h(1)=a;

          whilenotDone

          cnt=cnt+1;

          [curVal,slope]=f_and_df(a,b,n);%square

          yint=curVal-slope*a;

          aNew=-yintlope;%Thenewguess

          h(cnt)=aNew;

          if(abs(aNew-a)tol)%Breakifit"sconverged

          notDone=0;

          elseifcnt49%after50iterations,stop

          notDone=0;

          aNew=0;

          else

          a=aNew;

          end

          end

          x=aNew;

          function[f,df]=f_and_df(a,b,n)

          %Ourfunctionisf=a^n-bandit"sderivativeisn*a^(n-1).

          f=a^n-b;

          df=n*a^(n-1);

          2、在命令窗口輸入coder(圖形界面),回車,彈出MATLABCoderProject對(duì)話框;

          3、在New選項(xiàng)卡Name中輸入一個(gè)工程名nrt.prj;點(diǎn)擊Ok,彈出MATLABCoderMEXFunction對(duì)話框;

          4、在Overview選項(xiàng)卡中,點(diǎn)擊Addfiles,彈出對(duì)話框,選中nrt.m打開;

          5、添加三個(gè)輸入,分別為10、2、1e-9;兩個(gè)輸入也可以;

          6、選中Build選項(xiàng)卡,Outputtype中選擇cc++StaticLibrary;選中Generatecodeonly;

          7、點(diǎn)擊Moresettings,GeneralàLanguage選擇C++;Interface選項(xiàng)中去掉所有選項(xiàng);Close;

          8、點(diǎn)擊Build,進(jìn)行編譯;點(diǎn)擊Viewreport,彈出CodeGenerationReport對(duì)話框;

          9、利用vs2008建立一個(gè)控制臺(tái)應(yīng)用程序,將生成的相關(guān)文件nrt.cpp、nrt.h、newtonSearchAlgorithm.cpp、newtonSearchAlgorithm.h、nrt_types.h、rtwtypes.h拷到相關(guān)目錄下并添加到應(yīng)用程序中;

          10、分別在nrt.cpp、newtonSearchAlgorithm.cpp文件中添加#include“stdafx.h”;

          11、test.cpp文件中代碼為:

          #include"stdafx.h"

          #include"nrt.h"

          #includeiostream

          usingnamespacestd;

          int_tmain(intargc,_TCHAR*argv[])

          {

          doublevarargin_1=0,varargin_2=0,varargin_3=1e-9;

          cinvarargin_1varargin_2;

          doublenth_rt=0,iterations=0;

          doublehstry_data[50]={0};

          inthstry_sizes[1]={0};

          nrt(varargin_1,varargin_2,varargin_3,nth_rt,iterations,hstry_data,hstry_sizes);

          cout"nth_rt="nth_rtendl;

          cout"iterations="iterationsendl;

          cout"hstry_data="endl;

          for(inti=0;i50;i++)

          {

          couthstry_data[i]endl;

          }

          cout"hstry_sizes="hstry_sizes[0]endl;

          return0;

          }


          建筑施工腳手架工程施工方案園路的類型及鋪裝時(shí)應(yīng)注意的問題
          業(yè)務(wù)員面對(duì)陌生客戶怎樣開口說話常見死機(jī)原因剖析,電腦為什么S機(jī)
          百度筆試題:malloc/free與new/delete的區(qū)別加強(qiáng)安全生產(chǎn)監(jiān)督管理 努力推進(jìn)安全標(biāo)準(zhǔn)化達(dá)標(biāo)建設(shè)
          如何使用搜索技巧來成為一名高效的程序員淺議建筑工程項(xiàng)目成本管理
          行政樓外裝飾工程施工組織設(shè)計(jì)(三)電腦顯示器的相關(guān)設(shè)置與常見故障排除方法
          渡槽雙曲拱及槽身分部工程現(xiàn)澆施工方案安全工程師:風(fēng)險(xiǎn)情況交流
          淺談混凝土(房屋)裂縫的原因與防治改良程序需要的一些技巧11個(gè)
          信息發(fā)布:廣州名易軟件有限公司 http://m.jetlc.com
          • 勁爆價(jià):
            不限功能
            不限用戶
            1998元/年

          • 微信客服

            <output id="r87xx"></output>
          1. 
            
            <mark id="r87xx"><thead id="r87xx"><input id="r87xx"></input></thead></mark>
              • 成人视频网站在线观看18 | 91操操| 在线免费观看黄日本 | 99成人免费视频 | 国产成人综合久久久久久 | 三级av无码 | 无码69| 黄色三级在线观看 | 91CM229 跳蛋购物 突袭做爱 1080P - 美竹玲 | 黄色一级在线观看 |