| Contents | Index |
codegen options files fcn_1 args...
fcn_n args
codegen project_name
codegen options files fcn_1 args... fcn_n args translates the MATLAB® functions fcn_1 through fcn_n to a C/C++ static or dynamic library, executable, or to a MEX function. Optionally, you can specify custom files to include in the build. codegen applies the options to all functions fcn_1 through fcn_n. It applies args to the preceding function only (fcn_n). If you specify C++, MATLAB Coder™ wraps the C code into .cpp files so that you can use a C++ compiler and interface with external C++ applications. It does not generate C++ classes.
codegen project_name generates output for the MATLAB Coder project project_name. codegen generates a MEX function, C/C++ static or dynamic library or C/C++ executable depending on the project settings defined for project_name.
Each time codegen generates the same type of output for the same code or project, it removes the files from the previous build. If you want to preserve files from a previous build, copy them to a different location before starting another build.
files |
Space-separated list of custom files to include in generated code. You can include the following types of files:
If these files are on a path that contains non 7-bit ASCII characters, such as Japanese characters, codegen might not be able to find them. |
options |
Choice of compiler options. codegen gives precedence to individual command-line options over options specified using a configuration object. If command-line options conflict, the rightmost option prevails.
| ||||||||||||||||||||||||||||||||
project_name |
Name of the MATLAB Coder project that you want codegen to build. The project name must not contain spaces. |
Generate a MEX function from a MATLAB function that is suitable for code generation.
Write a MATLAB function, coderand, that generates a random scalar value from the standard uniform distribution on the open interval (0,1).
function r = coderand() %#codegen % The directive %#codegen indicates that the function % is intended for code generation r = rand();
Generate and run the MEX function. By default, codegen names the generated MEX function coderand_mex.
codegen coderand coderand_mex
Generate C executable files from a MATLAB function that is suitable for code generation. Specify the main C function as a configuration parameter.
Write a MATLAB function, coderand, that generates a random scalar value from the standard uniform distribution on the open interval (0,1).
function r = coderand() %#codegen r = rand();
Write a main C function, c:\myfiles\main.c, that calls coderand.
/*
** main.c
*/
#include <stdio.h>
#include <stdlib.h>
#include "coderand.h"
int main()
{
coderand_initialize();
printf("coderand=%g\n", coderand());
coderand_terminate();
return 0;
}Configure your code generation parameters to include the main C function, then generate the C executable.
cfg = coder.config('exe')
cfg.CustomSource = 'main.c'
cfg.CustomInclude = 'c:\myfiles'
codegen -config cfg coderandcodegen generates a C executable, coderand.exe, in the current folder, and supporting files in the default folder, codegen/exe/coderand.
This example shows how to specify a main function as a parameter in the configuration object coder.CodeConfig. Alternatively, you can specify the file containing main() separately on the command line. You can use a source, object, or library file.
Generate C library files in a custom folder from a MATLAB function with inputs of different classes and sizes. The first input is a 1-by-4 vector of unsigned 16-bit integers. The second input is a double-precision scalar.
Write a MATLAB function, mcadd, that returns the sum of two values.
function y = mcadd(u,v) %#codegen y = u + v;
Generate the C library files in a custom folder mcaddlib using the -config:lib option.
codegen -d mcaddlib -config:lib mcadd -args {zeros(1,4,'uint16'),0} Generate C library files from a MATLAB function that takes a fixed-point input.
Write a MATLAB language function, mcsqrtfi, that computes the square root of a fixed-point input.
function y = mcsqrtfi(x) %#codegen y = sqrt(x);
Define numerictype and fimath properties for the fixed-point input x and generate C library code for mcsqrtfi using the -config:lib option.
T = numerictype('WordLength',32, ...
'FractionLength',23, ...
'Signed',true)
F = fimath('SumMode','SpecifyPrecision', ...
'SumWordLength',32, ...
'SumFractionLength',23, ...
'ProductMode','SpecifyPrecision', ...
'ProductWordLength',32, ...
'ProductFractionLength',23)
% Define a fixed-point variable with these
% numerictype and fimath properties
myfiprops = {fi(4.0,T,F)}
codegen -config:lib mcsqrtfi -args myfiprops codegen generates C library and supporting files in the default folder, codegen/lib/mcsqrtfi.
Specify global data at the command line.
Write a MATLAB function, use_globals, that takes one input parameter u and uses two global variables AR and B.
function y = use_globals(u)
%#codegen
% Turn off inlining to make
% generated code easier to read
coder.inline('never');
global AR;
global B;
AR(1) = u(1) + B(1);
y = AR * 2;Generate a MEX function. By default, codegen generates a MEX function named use_globals_mex in the current folder. Specify the properties of the global variables at the command line using the -globals option. Specify that input u is a real, scalar, double, using the -args option.
codegen -globals {'AR', ones(4), 'B', [1 2 3 4]} ...
use_globals -args {0}Alternatively, you can initialize the global data in the MATLAB workspace. At the MATLAB prompt, enter:
global AR B; AR = ones(4); B=[1 2 3];
Compile the function to generate a MEX file named use_globalsx.
codegen use_globals -args {0}Generate output for a MATLAB Coder project, test_foo.prj, that includes one file, foo.m, and has it output type set to C/C++ Static Library.
codegen test_foo.prj
codegen generates a C library, foo, in the codegen\lib\foo folder.
Generate a MEX function for a function, displayState that has an input parameter that is an enumerated type.
Write a function, displayState, that uses enumerated data to activate an LED display, based on the state of a device. It lights a green LED display to indicate the ON state and lights a red LED display to indicate the OFF state.
function led = displayState(state)
%#codegen
if state == sysMode.ON
led = LEDcolor.GREEN;
else
led = LEDcolor.RED;
endDefine an enumeration LEDColor. On the MATLAB path, create a file named 'LEDColor' containing:
classdef(Enumeration) LEDcolor < int32
enumeration
GREEN(1),
RED(2),
end
endCreate a coder.EnumType object using a value from an existing MATLAB enumeration.
Define an enumeration sysMode. On the MATLAB path, create a file named 'sysMode' containing:
classdef(Enumeration) sysMode < int32
enumeration
OFF(0)
ON(1)
end
endCreate a coder.EnumType object from this enumeration.
t = coder.typeof(sysMode.OFF);
Generate a MEX function for displayState.
codegen displayState -args {t}
Use the coder function to create a MATLAB Coder project. The project provides a user interface that facilitates adding MATLAB files, defining input parameters, and specifying build parameters.
coder | coder.EnumType | coder.runTest | coder.typeof | fi | fimath | mex | numerictype

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2012- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |