Preparing MATLAB code for MATLAB Coder

advertisement
Feedback/Questions: email matlab-coder-expert@mathworks.com
Preparing MATLAB code
for MATLAB Coder:
a Quick Start Guide
 Note: do not add sub-functions as entry points: MATLAB Coder
automatically includes all the required sub-functions.
MATLAB Coder reads in the MATLAB file and lists the inputs to the top
level function.
(R2014b)
Step 2 – Get a Quick Overview of Unsupported Functions
MATLAB Coder parses your code and, if it detects any issue, displays the
following message in the MATLAB Coder GUI:
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 get your code ready for use with MATLAB Coder.
 You can also run the Code Generation Readiness tool outside of
MATLAB Coder on any file by selecting your file in the current
folder and right-clicking on “Check Code Generation Readiness”
Steps to getting your code ready




Create a MATLAB Coder project
Get a quick overview of unsupported functions in your code
Declare the types of the inputs
Ensure that the MATLAB code is compliant with the MATLAB
Coder subset
The Code Generation Readiness tool parses your whole code, including
sub-functions. It highlights supported and unsupported MATLAB functions,
as well as some unsupported language features. You will use this report in
step 2.
Details of the steps
Step 1- Create a MATLAB Coder Project
To create a MATLAB Coder Project, simply select “MATLAB Coder” from
the “APPS” tab or type:
A typical example of unsupported language features is cell arrays.
If your code includes cell arrays, you must eliminate them from the part
you want to generate code for, for example by using structures instead.
>> coder
and enter name the project, for example, “pr1”
Drag the top-level function to accelerate into the MATLAB Coder project.
1
Feedback/Questions: email matlab-coder-expert@mathworks.com
 Tip: You can also define the input types manually by example
(“zeros(3,4)” or by using variables defined in the workspace), or
by type (“double” and “3 x 4”).
Typical examples of supported language features include multidimensional arrays, structures, global and persistent variables, as well as
classes. See below for a list of supported functions.
 Note: The Code Generation Readiness tool only performs a
superficial scan of your code. It does not detect all possible issues
such as code style (see the “Typical Changes to the MATLAB
Code” section for examples)
You can define an input array to have:
a known size
a maximum size
an unknown size
or any combination
If the function you select includes unsupported functions, you can either
rewrite them or use coder.extrinsic. Also remember to check whether
there is a system object that performs a similar functionality. You can also
use coder.ceval to call an existing C implementation you may already have
for that function. See below for more detail on all these points.
2 x 10
:2 x :10
:inf x :inf
2 x :inf
 Tip: All changes to the project are saved automatically
Step 4 – Ensure that the MATLAB Code is Compliant with the
MATLAB Coder Subset and Generate the MEX File
Step 3- Declare the Types of the Inputs
Even if your ultimate goal is stand-alone C code, it is highly recommended
that you always start by generating a MEX file to verify the C code within
MATLAB first.
Define the type of the inputs with the “Autodefine types” feature: select a
script that calls the entry-point you selected and MATLAB Coder runs it to
determine the type of all input variables.
To generate the MEX file, switch to the “Build” pane in MATLAB Coder,
select “MEX function” and modify the output file name if desired. By
default, the output file name is your top level name with the “_mex” suffix.
Click the build button to generate the MEX file. You are likely to get error
messages, as your MATLAB code may not be MATLAB coder compliant yet.
The next section goes over some typical error messages and their
solutions. Open the report to get more detailed information. You can click
2
Feedback/Questions: email matlab-coder-expert@mathworks.com
on the “Variables” pane at the bottom to display information about what is
known about each variable in the code.
If you have a local variable “y = zeros(1,N)” where N is an input variable or
computed at run-time, you can still indicate a maximum size for ‘y’. To do
so, indicate a maximum value for N:
assert(N < 25); y = zeros(1,N);
 The ‘coder.varsize’ directive is the most over-used pragma by
beginners. You rarely need it. ‘coder.varsize‘ is only helpful when
a variable is declared with a given size (e.g. y = zeros(1,10)) but
you want to indicate that, although MATLAB Coder could think it
is 1 x 10, its size will change later in the code. This can typically
happen within a loop.
Typical Changes to the MATLAB Code
Here are four typical errors and their solution
Original code
Issue
Modified code
for i = 1:N
y(i,i) = i;
end
The array y is extended
dynamically without
being pre-defined
y = zeros(N,N);
for i = 1:N
y(i,i) = i;
end
y=zeros(1,10);
‘y’ is defined as double
but assigned a complex
value
y=complex(zeros(1,10);
y(3) = 1 + 2i;
y = 0.5*i;
Use ‘1i’ for sqrt(-1)
y = 0.5*1i;
y=
imrotate(x,a);
‘imrotate’ is not
supported. Use a
coder.extrinsic
declaration once in
each function where
‘imrotate’ is used
coder.extrinsic(‘imrot
ate’)
…
y = imrotate(x,a);
y(3) = 1+2i;
 Which MATLAB functions are supported for code generation?
You can find a list of all supported functions and system objects at:
http://www.mathworks.com/help/coder/language-supported-for-codegeneration.html
or as PDF at: http://www.mathworks.com/products/matlabcoder/files/matlab-coder-supported-functions-and-system-objects.pdf

Is there a webinar to watch to get an introduction to MATLAB
Coder?
(Also see FAQs below for
more about coder.extrinsic)
MATLAB to C Made Easy
http://www.mathworks.com/company/events/webinars/wbnr62736.html
 For imrotate, using the vision.GeometricRotator system
object from the Computer Vision System Toolbox, which is
supported for code generation, would be a better choice.
 Is there a command line equivalent to the GUI-based project?
Yes, the command name is “codegen”. You may also want to look up the
“coder.config” command to see how to set advanced options for the
codegen command
Miscellaneous Tips and FAQs

How to define a maximum size for an unknown size arrays
3
Feedback/Questions: email matlab-coder-expert@mathworks.com
 Can I export my MATLAB Coder GUI project settings to a configuration
object for use at the command line?
but it is very useful if you want to convert your design to fixed-point.
Refer to the buildInstrumentedMex documentation for further detail.
Yes, use the “Export settings…” option under the
For a more automated path to fixed-point code, select “Convert to fixedpoint at build time” in the “Overview pane”
menu
 Can I automatically convert my MATLAB Coder GUI project settings or
a configuration object to a MATLAB script?
Yes, use the “-tocode” option of the “coder” command
>> coder –tocode myproject –script myscript.m
 What do “coder.extrinsic” calls do?
When you use coder.extrinsic(“foo”), MATLAB Coder does not look at
“foo”. The exact behavior depends on the target:


For a MEX target (simulation), you can freely use coder.extrinsic.
The MEX file calls into MATLAB at runtime to execute “foo”
For a stand-alone C target, if you use coder.extrinsic, the call to
“foo” is removed from the generated C code. This only works
when the function removed does not impact the output of the C
code (for example “plot”)
Finally, note that, starting with R2012b, MATLAB Coder auto-inserts
extrinsic calls for a few functions (typically graphic ones, such as “plot”).
Consequently, it is not necessary to use coder.extrinsic for those functions.
 What is the “Instrumented MEX function” target under “Build” /
Output Type?
When you select that target, MATLAB Coder builds a MEX file that
subsequently records the minimum and maximum values of all internal
variables during simulation. This MEX file is slower than the standard one,
4
Download