Data References in the model
.rtw
File
model
.rtwData Reference Overview
Some records in a
file, such as
those corresponding to parameters and constant block I/O, can have extremely large data
value vectors embedded in them. Such a vector can cause significant memory overhead during
code generation because the values must be maintained as text in memory during this process. model
.rtw
To avoid such overhead, by default the Simulink® software does not write out the entire data value vector into
. Instead, it writes a key called
a data reference that can be used during code generation to access
the data directly from Simulink. If the data is not mutated during code generation, it is efficiently streamed
to disk when the actual code containing the data values is written out.model
.rtw
A data reference has the format
SLData(
, where
index
)
is a numeric value that tells
Simulink which data is being referenced. TLC directives such as
index
GENERATE_FORMATTED_VALUE
store data references in unexpanded format in
memory. When the generated code is written out to disk, the data values expand to the actual
values. To access the data vector of the model parameter written to the
ModelParameters
record in the
model.rtw
, use the LibGetParameterValue
function.
Control Data Reference Threshold
By default, if you do not specify a value for the model configuration parameter
RTWDataReferencesMinSize
, the code generator assigns the value
-1
to the parameter. In this case, the code generator writes a data
reference of a model parameter to the model.rtw
file
in place of a data vector. This optimization reduces the file read/write operations, which
improves the code generation time. When you specify a positive value for the
RTWDataReferencesMinSize
parameter, the code generator uses the value
as the threshold size based on which it writes data references to the
model.rtw
file in place of a data vector.
To change the maximum length of a vector that can appear literally in the file, use:
set_param(0, 'RTWDataReferencesMinSize', maxlen)
Simulink replaces a vector as long or longer than maxlen
with a data
reference when it creates
. Specify
model
.rtwmaxlen
as an integer or as inf
. Specifying
inf
disables data references. The complete value set of every vector,
however long, then appears literally in
and occupies text memory during
code generation.model
.rtw
Setting an explicit
affects only the current MATLAB® session. To set the value across sessions, include a
maxlen
set_param
command in your startup.m
file, or
automate execution of the command when MATLAB launches.
Expand Data References
You can explicitly expand a data reference by using the
GENERATE_FORMATTED_VALUE
built-in function with the optional third
expand
argument. Commands such as FEVAL
may cause a
data reference to be expanded to the full form.
Avoid Data Reference Expansion
Either turning off data references completely or expanding select parameters in TLC can cause significant text memory overhead during the code generation process. During most common code generation tasks, it is unnecessary to have the expanded data vector in memory and pay the price of the additional overhead. Avoid expanded data vectors unless no alternative exists.
Restart Code Generation
A
file that contains data
references cannot be used in isolation to restart a custom code generation process. The data
references within it become stale once the code generation process is completed. Attempting
to start a code generation process using only this file may result in unpredictable behavior
and memory segmentation faults.model
.rtw