Main Content

Access Multiple Model Instance C API Information

In certain scenarios, multiple instances of the same model can be used, such as when using instances as referenced models. Metadata information such as element data types and dimensions is consistent across all model instances. Your code accesses the model-specific metadata through the static C API map. In contrast, real-time values of model elements differ from one instance to another. Your code accesses the real-time data through the main C API map. All shared model data is accessible through a single, static model map, available to all instances. Meanwhile, each model instance accesses its unique, instance-specific data through its own private instance-specific map, serving as the exclusive gateway to this data. Instance-specific information is accessed by model instances through indices that are provided in shared C API arrays. The shared indices are identical to all instances of the same model, but each model instance uses them to reference entries in its own private copy of the instance-specific data arrays. In this way, the data separation principle is preserved.

Print Real-time Signal Values of Multiple Model Instances Example

This example demonstrates how to write code that uses instance-specific maps when there is more than one instance of the model.

The example code is also provided as standalone code (see Standalone C Code for Printing Real-time Signal Values of Multiple Model Instances Example) so that you can copy it more easily.

C Code for Printing Real-time Signal Values of Multiple Model Instances Example

DescriptionSample C Code with Direct Data Structure AccessSample 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 in which the structures you need to access are defined.
# include "capiDemoParent.h"
# include "rtw_modelmap.h"
# include "rtw_capi.h"
# include <stdio.h>
# include "capiMultiModelInstanceDemo.h"
# include "capiDemoParent.h"
# include "rtw_modelmap.h"
# include "rtw_capi.h"
# include <stdio.h>
# include "capiMultiModelInstanceDemo.h"
Declare variables to use for storing the map objects and the signals information.
const rtwCAPI_ModelMappingInfo *mainCapiMap;
uint_T childCount;

const rtwCAPI_ModelMappingInfo** childCapiMaps;
const rtwCAPI_ModelMappingStaticInfo *childStaticMap;

uint_T sigCount;
const rtwCAPI_Signals* capiSigArr;

void** dataAddrMap;

/* Index into the data map array */
uint_T dataMapIdx;

/* Assume each signal is a one dimension double. */
double* sigVal;
const rtwCAPI_ModelMappingInfo *mainCapiMap;
uint_T childCount;

const rtwCAPI_ModelMappingInfo** childCapiMaps;

uint_T sigCount;
const rtwCAPI_Signals* capiSigArr;

void** dataAddrMap;

/* Index into the data map array */
uint_T dataMapIdx;

/* Assume each signal is a one dimension double. */
double* sigVal;
Get the pointer to the main C API map of the parent model.
void printMultiModelInstanceSignalVals()
{
  mainCapiMap = &capiDemoParent_M->DataMapInfo.mmi;
void printMultiModelInstanceSignalVals()
{
  mainCapiMap = &rtmGetDataMapInfo(CapiSigsParams_M).mmi;
Get the information about the number of child models, and the pointer to the children C API map array. Then print the number of child models.
  childCount = mainCapiMap->InstanceMap.childMMIArrayLen;
  childCapiMaps = mainCapiMap->InstanceMap.childMMIArray;

  printf("There are %i child models.\n",childCount);
  childCount = rtwCAPI_GetChildMMIArrayLen(mainCapiMap);
  childCapiMaps = rtwCAPI_GetChildMMIArray(mainCapiMap);

  printf("There are %i child models.\n",childCount);
Use a for-loop to iterate over the child models. For each child model, get the signal count and the pointer to the signal array of the child model. When using direct data access, you first need to get a pointer to the static C API map of the child model.
  for(int childIdx=0 ; childIdx<childCount ; childIdx++)
  {
    childStaticMap = childCapiMaps[childIdx]->staticMap;
    sigCount = (childStaticMap->Signals).numSignals;
    capiSigArr = (childStaticMap->Signals).signals;
  for(int childIdx=0 ; childIdx<childCount ; childIdx++)
  {
    sigCount = rtwCAPI_GetNumSignals(childCapiMaps[childIdx]);
    capiSigArr = rtwCAPI_GetSignals(childCapiMaps[childIdx]);
Use the dataAddrMap field of the childCapiMaps entries to get the pointers to these (primitive type) data address arrays.
    dataAddrMap = childCapiMaps[childIdx]->InstanceMap.dataAddrMap;
    dataAddrMap = rtwCAPI_GetDataAddressMap(childCapiMaps[childIdx]);
Print the signal count for the current child model, then start iterating over the signals. For each signal, get the dataMapIdx to use for referencing into the signal array.
    printf("Child # %i has %i signals, with values: ",
                                       childIdx,sigCount);
    for(int sigIdx=0 ; sigIdx<sigCount ; sigIdx++)
    {
      dataMapIdx = capiSigArr[sigIdx].addrMapIndex;
    printf("Child # %i has %i signals, with values: ",
                                       childIdx,sigCount);
    for(int sigIdx=0 ; sigIdx<sigCount ; sigIdx++)
    {
      dataMapIdx = rtwCAPI_GetSignalAddrIdx(capiSigArr,sigIdx);
Use the dataMapIdx index to get the signal value, and print the value.
      sigVal = (double*)dataAddrMap[dataMapIdx];
      printf("%f", *sigVal);
      if(sigIdx < (sigCount-1))
      {
        printf(" , ");
      }
    }
    printf("\n");
  }
}
      sigVal = (double*)rtwCAPI_GetDataAddress(dataAddrMap,dataMapIdx);
      printf("%f", *sigVal);
      if(sigIdx < (sigCount-1))
      {
        printf(" , ");
      }
    }
    printf("\n");
  }
}

Here is a schematic illustration of the map structure that is accessed by the code in the example:

Standalone C Code for Printing Real-time Signal Values of Multiple Model Instances Example

 Ready-to-Copy Standalone C Code

Related Topics