Access Real-time C API Information
The real-time values of model elements are stored in computer memory. The number of
memory units used for storing the value of a specific element depends on the number of
components in the element. For example, if the signal mySig
is a
three-dimensional array with size 3-by-2-by-4, then its real-time value is stored in 24
memory units. The pointers to these memory cells are stored in a contiguous region of the C
API dataAddrMap
array, where the number of entries in that region is
equal to the number of components in the model element. So, if mySig
is
a signal with size 3-by-2-by-4, the pointers to the memory cells that hold the value of
mySig
are stored in 24 consecutive entries in
dataAddrMap
. Suppose that the 24 consecutive entries are entries 23
through 46 of the array. To access the three-dimensional real-time value of
mySig
, your code:
Gets the
dataTypeIndex
,dimIndex
, andaddrMapIndex
indices formySig
. To learn how to get these indices, see C Code for Accessing Signal C API Information.Uses the dimensions of
mySig
to calculate the number of components its value consists of. In this example,mySig
has 24 components. To learn how to obtain the dimensions of model elements, see C Code for Obtaining Element Dimensions Through C API.Gets the data type of
mySig
. The data type is required to read the signal values correctly. To learn how to obtain the data type of elements, see C Code for Accessing Data Type C API Information.References into the correct entry in
dataAddrMap
using the value ofaddrMapIndex
, which in this example is 23.Uses the pointers in entries 23 through 46 of
dataAddrMap
to obtain the 24 real-time value components ofmySig
.Note
The pointers in
dataAddrMap
are of the generic typevoid*
. Your code needs to downcast them to the actual pointer type they are, based on the signal data type, which your code previously obtained.
Print Real-time Signal Values Example
This example uses the instance specific map to print the actual value of the model signals.
The example code is also provided as standalone code (see Standalone C Code for Printing Real-time Signal Values Example) so that you can copy it more easily.
C Code for Printing Real-time Signal Values Example
Description | Sample C Code with Direct Data Structure Access | Sample C Code with Macros and Get-Functions |
---|---|---|
Include the generated model header so you can access the real-time
model object. Also include rtw_modelmap.h and
rtw_capi.h so in which the structures you need
to access are defined. |
|
|
Declare variables to use for storing the map objects and the signals information. |
|
|
Get the pointers to the C API model maps (see Obtain Access to C API Maps Example for more details). Then extract the number of signals that are included in the generated C API code, and obtain the pointer to the signal structure array. |
|
|
To get the pointers to the (primitive type) data addresses
structure array, use the instance map pointer to access the
dataAddrMap field of the map. |
|
|
Start a for -loop to go over the model
signals. |
|
|
For each signal, get the index that references into the data map array. |
|
|
Use the index to access the corresponding entry in the data map array and print the value of each signal. |
|
|
Standalone C Code for Printing Real-time Signal Values Example
Ready-to-Copy Standalone C Code
Here is a schematic illustration of the map structure that is accessed by the code in the example: