MATLAB Coder Code Generation Quick Start Guide
MATLAB Coder Code Generation Quick Start Guide
com
(R2012a) Whats the difference between Static/Dynamic Library and Executable? As far as the generated C code is concerned, there is no difference. Static (resp. Dynamic) Library Executable MATLAB Coder builds a static (resp. dynamic) library, which you can link against in a C-based project. Location: codegen/lib/<topLevelName> (resp. dll) You must provide a main function. MATLAB Coder then builds an executable file that calls the generated code. Location: codegen/exe/<topLevelName>
MATLAB Coder lets you generate C code from your MATLAB code. You can generate stand-alone ANSI-C code or C code that includes the necessary interfaces to run within MATLAB as compiled MEX file. This quick start guide focuses on how to generate stand-alone ANSI-C code. Refer to Preparing MATLAB Code for MATLAB Coder: a Quick Start Guide [1] for information about how to make your MATLAB code compliant.
Logical array indexing is the ability to perform operations on sub-elements of an array based on a logical index. For example: you can clip all elements larger than 255 with: x(x>255) = 255; Rewriting it as follows generally yields better code: for ii=1:numel(x), if (x(ii)>255), x(ii) = 255; end, end
Add information about array sizes where needed. For example: assert(N < 25); y = zeros(1,N); See also Optimize indexing below for another practice that may help. Ensure that the generated code passes arguments by reference Optimize indexing
MATLAB Coder may not be able to recognize that (i:i+9) has a fixed length of 10. Rewrite it as i + (0:9) to give the necessary hint to MATLAB Coder. Enforce scalar typing
FAQs
Do I need Embedded Coder? Embedded Coder lets you customize the C code generated for a standalone target. The main advantages to Embedded Coder are the ability to: Target Code Replacement Libraries (CRL) for embedded targets Generate code for specific targets (e.g. TI DSP) and co-simulate with IDEs Generate reentrant code Customize the code style such as defining name rules for variables
Memory Layout
MATLAB is column-major: consecutive elements in a 2D matrix correspond to elements in a column. On the other hand, C is row-major. The C code that MATLAB Coder generates mimics the MATLAB behavior. Consequently, if any of the input (resp. output) arguments is a multidimensional (2D or more) matrix, you may need to transpose your input (resp. output) to match an external C testbench.
MATLAB Coder does not directly support load or fopen. While there is limited direct support for file I/O (Video and Audio file reader system objects), you can find an example of how to read from and write to generic files by looking at the example for coder.opaque Command-line equivalent
Argument Types
Input and output arguments map to the following C function arguments Fixed-size arrays Max-size array not dynamic Dynamically allocated arrays Pointer to array: x[10] Pointer to array along with array with actual runtime sizes: x_data[10] and x_sizes[2] emxArrays, which are special types defined in a .h file produced. MATLAB Coder provides helper functions to create and destroy those arrays. Refer
You can generate code and set all options from the command line. See codegen and coder.config Clashing function names when generating code for multiple functions
If you generate code independently for several functions, MATLAB Coder may generate C functions that have the same name but different behavior for some of the sub-functions. When you try to merge the files for all functions into one directory, this may lead to a conflict.
Useful Functions
Refer to the documentation for details coder.ceval coder.cstructname coder.inline Call an external C function, which you already have, from the generated C code Specify the name of the C structure generated for your MATLAB structure Enforce of prevent inlining of sub-functions. When you use coder.inline(always) in a MATLAB sub-function, the corresponding code is inlined in the generated C code Reserved variable, which is empty when running the MATLAB interpreter. Lets you define different processing in code generation and MATLAB mode: if isempty(coder.target) % This happens in MATLAB else % This happens in code generation end Suppresses the initialization statement for a variable defined as zeros. This results in slightly faster code, but you must ensure that all slices of the variable are written to before read from Create a configuration object to set all the necessary options when generating C code from the command line with the codegen command
MATLAB Coder lets you replace operators and functions with targetspecific implementations. This capability is described in the documentation under the heading Code Replacement Library (CRL) and requires Embedded Coder. Generating re-entrant / thread-safe code? coder.nullcopy
This capability requires Embedded Coder. Under More Settings -> Memory Generating C++ code? coder.config
MATLAB Coder always generates C code. When you select the C++ option, MATLAB Coder generates C code (with a .cpp extension) that a C++ compiler can compile. Select C++ under More Settings -> General -> Language