MATLAB Code Patterns That Require a Nonempty Initialize Function in Generated Code - MATLAB & Simulink (original) (raw)
Issue
When generating standalone code from MATLAB® code, you choose not to generate the initialize function:
- In a coder.EmbeddedCodeConfig object,
IncludeInitializeFcn
is set tofalse
. - Alternatively, in the MATLAB Coderâ„¢ app, on the tab, is set to
No
.
Also, you ensure that the custom code to appear in the generated initialize function is empty:
- In a coder.EmbeddedCodeConfig object,
CustomInitializer
is set to''
. - Alternatively, in the MATLAB Coder app, on the tab, for initialize function is empty.
You still get this error message:
The initialize function is not empty, but the IncludeInitializeFcn configuration setting requests that the initialize function be omitted.
Solution
In certain situations, the code generator determines that the initialize function must be nonempty and produces code for the initialize function. This code generation can occur even if the custom code for the initialize function that you specify is empty. Not including this generated code in the initialize function causes the generated entry-point functions to operate on an invalid state. In such situations, if you choose to not generate the initialize function, the code generator produces an error message.
To fix this issue, include the initialize function in your generated code by doing one of the following:
- In a coder.EmbeddedCodeConfig object, set
IncludeInitializeFcn
totrue
(this value is the default value). - In the MATLAB Coder app, on the tab, set to
Yes
(this setting is the default setting).
Examples of MATLAB code patterns that require a nonempty initialize function in the generated code are:
- Your MATLAB code contains an operation that can generate nonfinite values (
Inf
orNaN
). For example, define a MATLAB functionfoo
that calls factorial. Thefactorial
function grows quickly and returnsInf
for inputs greater than a certain threshold. For an input of typedouble
, the threshold is170
. Executingfactorial(171)
in MATLAB returnsInf
.
function y = foo(a)
y = factorial(a);
end
Generate a static library forfoo
. In thecoder.EmbeddedCodeConfig
objectcfg
, set the parameterCustomInitializer
to an empty character array. Set the parameterIncludeInitializeFcn
totrue
. These values are the default values for the parameters.
cfg = coder.config('lib');
cfg.CustomInitializer = ''; %These are the default values
cfg.IncludeInitializeFcn = true;
codegen -config cfg foo -args {1}
The code generator automatically produces a nonempty initialize functionfoo_initialize
that calls supporting code for nonfinite data. This function callsrt_InitInfAndNaN
, which is defined in another generated filert_nonfinite.c
.
/* Include Files */
#include "foo_initialize.h"
#include "foo.h"
#include "foo_data.h"
#include "rt_nonfinite.h"
/* Function Definitions /
/
- Arguments : void
- Return Type : void
/
void foo_initialize(void)
{
rt_InitInfAndNaN();
isInitialized_foo = true;
}
/ - File trailer for foo_initialize.c
- [EOF]
*/ - Your MATLAB code uses global or persistent variables. For example, define this MATLAB function:
function y = bar
global g
y = g;
end
Generate a static library forbar
. Specify the initial value ofg
as1
. In thecoder.EmbeddedCodeConfig
objectcfg
, set the parameterCustomInitializer
to an empty character array. Set the parameterIncludeInitializeFcn
totrue
. These values are the default values for the parameters.
cfg = coder.config('lib');
cfg.CustomInitializer = ''; %These are the default values
cfg.IncludeInitializeFcn = true;
codegen -config cfg -globals {'g',1} bar
The code generator automatically produces a nonempty initialize functionbar_initialize
that initializesg
.
/* Include Files */
#include "bar_initialize.h"
#include "bar.h"
#include "bar_data.h"
/* Function Definitions /
/
- Arguments : void
- Return Type : void
/
void bar_initialize(void)
{
g = 1.0;
isInitialized_bar = true;
}
/ - File trailer for bar_initialize.c
- [EOF]
*/