从MATLAB代码生成独立c语言代码.doc_第1页
从MATLAB代码生成独立c语言代码.doc_第2页
从MATLAB代码生成独立c语言代码.doc_第3页
从MATLAB代码生成独立c语言代码.doc_第4页
从MATLAB代码生成独立c语言代码.doc_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Matlab 用 help lsqcurvefitMATLAB 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 bc = a * b其中,%#codegen可以防止出现警告错误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,GeneralLanguage选择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 using namespace std;int _tmain(int argc, _TCHAR* argv) double a = 0.0, b = 0.0, c = 0.0; cinab; c = foo(a, b); coutc = cendl; 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 = varargin1;n = varargin2;if nargin = 3 tol = 1e-9;else tol = varargin3;endif 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);endnewtonSearchAlgorithm.m:function x, h = newtonSearchAlgorithm(b, n, tol) %#codegen%Given, a, this function finds the nth root of a%number by finding where: xn-a = 0coder.inline(never); %使其生成一个单独的c+文件notDone = 1;aNew = 0; %Refined Guess Initializationa = 1; %Initial Guesscnt = 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) 49 %after 50 iterations, stop notDone = 0; aNew = 0; else a = aNew; endendx = aNew;function f, df = f_and_df(a, b, n)%Our function is f=an-b and its derivative is n*a(n-1).f = an-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 using namespace std;int _tmain(int argc, _TCHAR* argv) double varargin_1 = 0, varargin_2 = 0, varargin_3 = 1e-9; cinvarargin_1varargin_2; double nth_rt = 0, iterations = 0; double hstry_data50 = 0; int hstry_sizes1 = 0; nrt(varargin_1, varargin_2, varargin_3, &nth_rt, &iterations, hstry_data, hstry_size

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论