Coder generated using MATLAB C / C ++ code that illustrates the steps from MATLAB

MATLAB Coder from MATLAB code generated independently, readable, portable C / C ++ codes .

Coder MATLAB code used to generate three steps: preparing MATLAB algorithm for generating a code; compatibility check MATLAB code (some matlab code statement is not considered a c / c ++ Code); generating source code or end-use MEX .

Generated using MATLAB Coder c ++ code and verify in vs2008:

A simple example, multiplying the number two :

1, the installation matlab2011a or later;

2, to generate a simple foo.m file;

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

%This function muliplies a and b

c = a * b

Which, % # CodeGen warning can prevent errors

3, in the command window, enter mex -setup, select an existing compiler;

4, in the command window, type coder (GUI), Enter, MATLAB Coder Project pop-up dialog box;

5. In the New Name tab in a project name foo.prj; click Ok, MATLAB Coder MEX Function pop-up dialog box;

6, in the Overview tab, click Add files, the pop-up dialog box, select foo.m open;

7, click the variable a, select Define by Example ..., MATLAB Coder Define by Example pop-up dialog box input 5 in MATLAB Expression, the click OK; b is also the same variable corresponding operation, input 6;

8, select the Build tab, Output type selected in c / c ++ Static Library; select Generate code only;

9, click More settings, GeneralàLanguage choose C ++; Interface options to remove all the options; Close;

10, Click the Build, compile; click View report, the pop-up dialog box Code Generation Report In this case, the variables a, b, c displays the corresponding variable information;

11, using the vs2008 build a console application, the resulting documents foo.h, foo.cpp, rtwtypes.h, foo_types.h Kaodao related directory and add to the application;

12, add #include "stdafx.h" in foo.cpp file; // VC ++ 6.0 can not find the library, the actual test, ignored

13, test.cpp file code: // write their own test of a file

// # include "stdafx.h" // Ignore

#include "foo.h"

#include <iostream>

 

using namespace std;

 

int _tmain (int argc, _TCHAR * argv []) // test to the actual main ()

{

 

double a = 0.0, b = 0.0, c = 0.0;

 

cin>>a>>b;

 

c = foo(a, b);

 

cout<<"c = "<<c<<endl;

 

return 0;

}

 

A more complex example, a number of n - th root of :

1, two .m file:

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');% allowed c ++ to generate a single file

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 �ter 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, in the command window, type coder (GUI), Enter, MATLAB Coder Project pop-up dialog box;

3. Enter the New Name tab in a project name nrt.prj; click Ok, MATLAB Coder MEX Function pop-up dialog box;

4, in the Overview tab, click Add files, the pop-up dialog box, select nrt.m open;

5, three input add, respectively 10,2,1e-9; two inputs may be;

6, select the Build tab, Output type selected in c / c ++ Static Library; select Generate code only;

7, click More settings, GeneralàLanguage choose C ++; Interface options to remove all the options; Close;

8, click Build, compile; click View report, Code Generation Report pop-up dialog box;

9, use vs2008 build a console application, the resulting documents nrt.cpp, nrt.h, newtonSearchAlgorithm.cpp, newtonSearchAlgorithm.h, nrt_types.h, rtwtypes.h Kaodao relevant directory and add it to your application ;

10, respectively, add #include "stdafx.h" in nrt.cpp, newtonSearchAlgorithm.cpp file;

11, test.cpp file code:

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

}

 

reference:

1, MATLAB Coder webinar recording of video http://v.youku.com/v_show/id_XMzA3MTA3NDQ4.html

2、http://www.mathworks.cn/products/matlab-coder/

Original:  https://blog.csdn.net/bhj5787/article/details/17093961

Guess you like

Origin blog.csdn.net/xuxinrk/article/details/93382659