In MATLAB® code that you want to convert to single precision,
it is a best practice to use integers for index variables. However,
if the code does not use integers for index variables, when possible
convertToSingle tries to detect
the index variables and select
int32 types for
Do not use
to define the properties of input arguments.
Do not use
to test the type of a variable. For example, do not use
Do not initialize MATLAB class properties in the
Instead, use the constructor to initialize the class properties.
Separate your core algorithm from other code that you use to test and verify the results. Create a test file that calls your double-precision MATLAB algorithm. You can use the test file to:
Automatically define properties of the top-level function inputs.
Verify that the double-precision algorithm behaves as you expect. The double-precision behavior is the baseline against which you compare the behavior of the single-precision versions of your algorithm.
Compare the behavior of the single-precision version of your algorithm to the double-precision baseline.
For best results, the test file must exercise the algorithm over its full operating range.
MATLAB code that you want to convert to single precision must comply with code generation requirements. See MATLAB Language Features Supported for C/C++ Code Generation.
To help you identify unsupported functions or constructs in
your MATLAB code, add the
to the top of your MATLAB file. When you edit your code in the MATLAB editor,
the MATLAB Code Analyzer flags functions and constructs that
are not supported for code generation. See Check Code Using the MATLAB Code Analyzer. When you use the MATLAB
the app screens your code for code generation readiness. At the function
line, you can use the Code Generation Readiness Tool. See Check Code Using the Code Generation Readiness Tool.
-argsOption to Specify Input Properties
When you generate single-precision MATLAB code, if you
specify a test file, you do not have to specify argument properties
-args option. In this case, the code generator
runs the test file to determine the properties of the input types.
However, running the test file can slow the code generation. It is
a best practice to pass the properties to the
convertToSingle does not run the test
file to determine the argument properties. If you have a MATLAB
you can use
coder.getArgTypes to determine the
argument properties. For example:
types = coder.getArgTypes('myfun_test', 'myfun'); scfg = coder.config('single'); convertToSingle -config scfg -args types myfun
When you use the convertToSingle function to
generate single-precision MATLAB code, enable numerics testing
and I/O data logging for comparison plots. To use numerics testing,
you must provide a test file that calls your MATLAB function.
To enable numerics testing and I/O data logging, create a
LogIOForComparisonPlotting properties. For
scfg = coder.config('single'); scfg.TestBenchName = 'mytest'; scfg.TestNumerics = true; scfg.LogIOForComparisonPlotting = true;