Main Content

Migration of Model Data Configurations to Code Mappings

Starting in R2020b, the recommended approach to configure model data for code generation is to use the Code Mappings editor or code mappings API. From one interface, you can configure default settings for categories of data, and then override those settings, as needed, for specific data elements. For parameters, you can specify whether they are inlined or tunable by default. When producing code for data, the code generator uses the code definitions that you specify to determine how to represent the data in the generated code.

When you open a model created in a release before R2020b, Simulink® migrates code definitions specified in the model to the model code mappings.

Effects of Migration

For data configured within a model, the migration process replaces existing storage class specifications with equivalent code mappings. Data configured for code generation within a model includes data represented by:

  • Outport blocks

  • Signal lines

  • Block states

  • Data stores

  • Parameter objects in the model workspace

This table lists specific model conditions and their corresponding migration results.

ConditionMigration Results
Model is a library model.Simulink discards the code configuration associated with data configured within the model.
Data element is configured for code generation within the model and the data element is associated with a Simulink.CoderInfo object, the StorageClass property for that object is set to Custom, and the CustomStorageClass property is set to Default.Simulink changes the storage class setting from Custom (Default) to ExportedGlobal and displays a warning.

Model uses data objects configured for use with the TypeQualifier storage class property for a built-in storage class, such as ExportedGlobal or ImportedExtern. You might have set the property as:

  • CoderInfo.TypeQualifier property for data objects in a workspace or data dictionary

  • Port parameter RTWStorageTypeQualifier

  • Block parameter RTWStateStorageTypeQualifier for Data Store Memory, Discrete Filter, Discrete State-Space, Discrete-Time Integrator, Discrete Transfer Fcn, Discrete Zero-Pole, and Memory blocks

Simulink issues a warning indicating that the property is no longer supported. Other interfaces are available for achieving the same results.
Model uses the Simulink.CoderInfo object Alignment property for data configured within the model.Simulink issues a warning indicating that the property is no longer supported for model-specific data. You can use the property for data stored in the base workspace or a data dictionary.
You initialize the model workspace for the model from an external data source, such as a MAT-file, MATLAB® file, or data dictionary.Simulink moves code configuration for the parameter object to the code mappings for that model.
For an individual subsystem from which you generate code and executable programs by right-clicking the Subsystem block, you configure the storage class and storage class properties for the subsystem input and output signals outside of the subsystem. Generated code does not include variables for the input and output interface data.

If you perform these operations, you might need to take action:

  • Use the Simulink Editor to copy a block line to another model. Simulink does not copy the code configuration information associated with the copied modeling element. For more information, see Copy Blocks and Signal Lines Between Models (Embedded Coder).

  • Switch the system target file setting for a model between a GRT-based and ERT-based system target file. Simulink ignores the code configuration information for the system target file of the source version of the copied model. For more information, see Switch Between GRT- and ERT-Based System Target Files (Embedded Coder).

Considerations Before Migrating

Before migration, consider the preparation suggested in the following sections if these conditions exist:

  • Data configured within a library model

  • Storage class TypeQualifier property

  • Data alignment for code generation

  • Code configuration for parameter objects initialized in the model workspace

  • Code configuration for subsystem I/O interface

Data Configured Within Library Model

If your model is a library model, reconfigure code generation for data. Reconfigure code generation for data in the context of models that use the library (see Code Interface Configuration).

To avoid losing the code configuration for data configured within the model, in an earlier release, load the model and create and run a MATLAB script that:

  1. Loads the model data.

  2. Finds data configured for code generation within the model.

  3. Converts the representation of the data to data objects that you store in an external source, such as a MAT-file, MATLAB file, or data dictionary.

  4. Updates the model to use the external data objects.

Storage Class TypeQualifier Property

If your model configures data objects with built-in storage classes, such as ExportedGlobal and ImportedExtern, and uses the storage class TypeQualifier property, when you load the model in release R2020b or later, Simulink issues a warning. To eliminate the warning and update a model to use a supported alternative, in an earlier release, load the model and create and run a MATLAB script that:

  1. Loads the model data objects from the model workspace, base workspace, or data dictionary.

  2. Finds data objects with the TypeQualifier property set to a non-empty string value.

  3. Changes the storage class setting to a storage class predefined with the required type qualifier (for example, storage class Const includes qualifier const in data declarations and definitions).

This example script loads model data objects from the base workspace, finds instances of data configured with the storage class TypeQualifier property, and changes the storage class setting to Const.

vars = who;

  for idx = 1:length(vars)
    thisVar = evalin('base',vars{idx});
    if (isa(thisVar,'Simulink.Parameter') && (isequal(thisVar.CoderInfo.TypeQualifier, 'const')))
      thisVar.CoderInfo.TypeQualifier = '';
      thisVar.CoderInfo.StorageClass = 'Custom';
      thisVar.CoderInfo.CustomStorageClass = 'Const';
    end
  end

Data Alignment for Code Generation

To preserve data alignment information configured for a model, represent data with data objects outside of the model. For example, open the model in a previous release of Simulink and perform the actions listed in this table.

Type of Data ElementAction
Parameter object in the model workspaceUse the Model Explorer to move the object from the model workspace to the base workspace or a data dictionary. Then, set the storage class and alignment properties of the object. Save the model.
Signal lineOpen the Signal Properties dialog box, specify a name for the signal and select Signal name must resolve to Simulink signal object (storage class must be Auto). Create a Simulink.Signal object in the base workspace or a data dictionary and set the storage class and alignment properties of the object. Save the model.
Block stateOpen the Block Parameters dialog box, specify a name for the state and select State name must resolve to Simulink signal object (storage class must be Auto). Create a Simulink.Signal object in the base workspace or a data dictionary and set the storage class and alignment properties. Save the model.

Alternatively, create and run a MATLAB script that:

  1. Loads the model data.

  2. Finds data objects that set Simulink.CoderInfo.Alignment.

  3. Converts the representation of the data to data objects that you store in an external source, such as a MAT-file, MATLAB file, or data dictionary.

  4. Sets Simulink.CoderInfo object property Alignment for the new data objects.

For example, this script checks whether Alignment is set for parameter, lookup table, and breakpoint data objects and captures the data objects found in MAT-file savedData.mat.

filename = 'savedData.mat';
varList = who;
tosaveVars = {};
for idx = 1 : length(varList)
   variable = eval(varList{idx});
   if ((isa(variable, 'Simulink.Parameter') || ...
       isa(variable, 'Simulink.LookupTable') || ...
       isa(variable, 'Simulink.Breakpoint')) && ...
       variable.CoderInfo.Alignment ~= -1)
       tosaveVars{end+1} = varList{idx};       
   end
end
if ~isempty(tosaveVars)
  save('savedData.mat', tosaveVars{:});
end

Evaluate the script in the model workspace with these commands:

mws = get_param(bdroot, 'ModelWorkspace');
evalin(mws, 'SaveMWSParamsWithAlignment');

For parameter objects in the model workspace, alternatively, you can use the Model Explorer interface to move the objects from the model workspace to the base workspace or a data dictionary. Then, select the Alignment property for the storage class.

For more information, see Optimize Performance of Memory Access by Using Data Alignment (Embedded Coder).

Code Configuration for Parameter Objects Initialized in Model Workspace

After loading a model in release R2020b or later and initializing the model workspace for the model from an external data source, such as a MAT-file, MATLAB file, or data dictionary, Simulink:

  • Saves the contents of the Simulink.CoderInfo object to the model code mappings.

  • Removes the Simulink.CoderInfo object from the Simulink.Parameter object defined in the external data source.

Use the Code Mappings editor or the code mappings API to get and set parameter code configuration settings. See Code Interface Configuration.

Code Configuration for Subsystem I/O Interface

In release R2020b or later, for individual subsystems from which you generate code and executable programs by right-clicking the Subsystem block, to include signal data in the generated subsystem code interface, configure the storage class and storage class properties for the subsystem input and output signals within the subsystem. If you configure the signals outside of the subsystem, the generated code does not include variables for the input and output interface data.

For more information, see Generate Code and Executables for Individual Subsystems.

Copy Blocks and Signal Lines Between Models

Starting in R2020b, when you use the Simulink Editor to copy a block or signal line to another model, Simulink does not copy the code configuration information associated with the copied modeling element.

To work around this change, copy and paste the code configuration information for data elements by using the code mappings API.

  1. Open the source and destination models for the copy operation.

  2. Use the code mappings API to get the code mappings object for the source model.

  3. Use the code mappings API to get code mapping information for the model element that you are copying.

  4. If a code mappings object does not exist for the destination model, use the code mappings API to create one.

  5. For the model element that you are copying to the destination model, use the code mappings API to set code mapping information to the same values as for the source model.

  6. Close the source model.

  7. Save and close the destination model.

For example:

.
.
.
srcMdl = 'my_src_mdl';
destMdl = 'my_dest_mdl';

open_system(srcMdl);
open_system(destMdl);

% Get data configuration from source model
srcCM = coder.mapping.api.get(srcMdl);
srcSC = getSignal(srcCM, srcPort, 'StorageClass')

% Set data configuration for destination model to setting of source model
destCM = coder.mapping.api.get(destMdl);
setSignal(destCM, destPort, 'StorageClass', srcSC);

close_system(srcMdl);
save_system(destMdl);
close_system(destMdl);
.
.
.

Switch Between GRT- and ERT-Based System Target Files

Starting in R2020b, when you switch the system target file setting for a model between a GRT-based and ERT-based system target file, Simulink applies alternative code configurations for each system target file. This switch can occur if you:

  • Alternate between the Simulink Coder™ and Embedded Coder® apps.

  • Change the active configuration set for a model.

  • Change the setting of model configuration parameter System target file.

To preserve the code configuration information for data configured within the model, create and run a MATLAB script that uses the code mappings API to copy relevant code mappings.

  1. Open the model with the source system target file and code mappings that you want to preserve.

  2. Use the code mappings API to get the code mappings object for the model and get data mappings objects for the data elements.

  3. Use the code mappings API to get the code mappings object for the model and set data mappings objects for the data elements.

  4. Save and close the model.

This script fragment shows how to use the code mappings API to preserve the storage class of a signal object when switching from a GRT system target file to an ERT system target file.

.
.
.
mymdl = 'my_shared_mdl';

open_system(mymdl);

% Get data configuration for Simulink Coder (GRT-based) code generation
scCodeMapping = coder.mapping.api.get(model, 'SimulinkCoderC'); 
scStorageClass = getSignal(scCodeMapping, srcPort, 'StorageClass') 

% Set data configuration for Embedded Coder (ERT-based) code generation
ecCodeMapping = coder.mapping.api.get(model, 'EmbeddedCoderC'); 
setSignal(ecCodeMapping, srcPort, 'StorageClass', scStorageClass)

save_system(mymdl);
close_system(mymdl);

.
.
.

Workflow Changes

Starting in R2020b, configure model data elements for code generation by using the Code Mappings editor or code mappings API instead of or in addition to creating and configuring Simulink data objects. Use this workflow:

  1. Open the Simulink Coder app.

  2. In the Code Mappings editor, configure default behavior for storage representation.

  3. Decide whether to override default configuration settings for individual data elements. If you choose not to override settings, skip to step 8.

  4. Identify MATLAB variables that you want to configure individually and store the configurations in the model workspace. In the Model Explorer, convert the variables to Simulink.Parameter objects. The resulting data objects appear in the Code Mappings editor as model parameters.

  5. Identify signals representing algorithm data that you want to monitor while the generated code executes and configure individually. For each signal that you identify, do one of the following:

  6. Configure individual data elements. In the Code Mappings editor, click a category tab, select a data element, and set the storage class. In the Property Inspector, set storage class property values. To change the setting of model configuration parameter Default parameter behavior click the link indicating the behavior when the storage class is set to Auto. In the Model Configuration Parameters dialog box, change the parameter setting to Tunable or Inline.

  7. To view external data objects that are stored in the base workspace or a data dictionary, in the Code Mappings editor, click the Code Mappings editor Refresh button.

  8. Generate and review code.

Related Topics