Preserve Unused Class Properties in Generated C/C++ Code
C/C++ code generated by Embedded Coder® contains unused class properties or structure fields. Removing unused properties or fields helps improve the memory footprint and run time of the code. By default, the code generator removes properties or fields that are unused in the generated C/C++ code.
This feature applies to standalone code generation. You can preserve the unused properties or fields in generated C/C++ code by using the MATLAB® Coder™ app or at the command line.
Preserve Unused Class Properties or Structure Fields by using MATLAB Coder App
To preserve unused properties or fields:
Open the MATLAB Coder app.
On the Generate Code page, click More Settings.
On the Memory tab, select the Preserve unused fields and properties check box.
Preserve Unused Class Properties or Structure Fields at the Command Line
To preserve the unused properties or fields, set the configuration parameter
PreserveUnusedStructFields
to true
and use
the configuration object cfg
in the codegen
command.
cfg = coder.config('lib');
cfg.PreserveUnusedStructFields = true;
Examples
The following examples demonstrate the usage of the
PreserveUnusedStructFields
configuration option to generate
C/C++ code.
Generate C++ Code for MATLAB Classes
This table compares the generated C++ code for myClass
, with
unused properties removed and with unused properties preserved. In order to preserve
the unused class property c
, you must assign a value to the
property c
in the class constructor.
MATLAB Code | Unused Class Properties Removed (default) | Unused Class Properties Preserved |
---|---|---|
% Entry-point function function y = myAdd(n) %#codegen o = myClass(n); y = o.a + o.b; end classdef myClass properties a b c end methods function obj = myClass(x) coder.inline('never') obj.a = x; obj.b = x + 1; obj.c = x + 2; end end end | Code generation command cfg = coder.config('lib'); cfg.TargetLang = "C++"; codegen myAdd -args {5} -config cfg -report Generated C++ Code class myClass { public: void init(double x); double a; double b; }; void myClass::init(double x) { a = x; b = x + 1.0; } | Code generation command cfg = coder.config('lib'); cfg.PreserveUnusedStructFields = true; cfg.TargetLang = "C++"; codegen myAdd -args {5} -config cfg -report Generated C++ Code class myClass { public: void init(double x); double a; double b; double c; }; void myClass::init(double x) { a = x; b = x + 1.0; c = x + 2.0; } |
Generate C Code for MATLAB Structures
This table compares the generated C code for myStruct
, with
unused fields removed and with unused fields preserved.
MATLAB Code | Unused Structure Fields Removed (default) | Unused Structure Fields Preserved |
---|---|---|
% Entry-point function function out = myStruct(n) %# codegen s.a = [n n n]; s.b = n+2; s.c = n; out = myAdd([s s]); end function out = myAdd(s) coder.inline('never'); out = s(1).b + s(2).b; end | Code generation command cfg = coder.config('lib'); codegen myStruct -args {0} -config cfg -report Generated C Code typedef struct { double b; } struct_T; /* * Arguments : double n * Return Type : double */ double myStruct(double n) { struct_T b_s[2]; struct_T s; s.b = n + 2.0; b_s[0] = s; b_s[1] = s; return myAdd(b_s); } | Code generation command cfg = coder.config('lib'); cfg.PreserveUnusedStructFields = true; codegen myStruct -args {0} -config cfg -report Generated C Code typedef struct { double a[3]; double b; double c; } struct_T; /* * Arguments : double n * Return Type : double */ double myStruct(double n) { struct_T b_s[2]; struct_T s; s.a[0] = n; s.a[1] = n; s.a[2] = n; s.b = n + 2.0; s.c = n; b_s[0] = s; b_s[1] = s; return myAdd(b_s); } |
Usage Notes
When
PreserveUnusedStructFields
is set tofalse
, the code generator preserves properties or fields of the entry-point function input and output arguments and values passed tocoder.ceval
.When
PreserveUnusedStructFields
is set totrue
, the code generator removes properties or fields whose data types are unknown.