Main Content

Generate Code to Conform to Software Architecture by Sharing and Copying Default Settings Between Models

In a project where you generate code from many models, you can configure the models with the same default code settings so that they generate code that conforms to your software architecture standards. These settings include:

  • Configuration parameter settings

  • Code interface definitions such as storage classes

  • Code mappings that specify code definitions for categories of model data and functions

As projects expand and you and your colleagues create more models, manually configuring the default settings for each new model takes time. To reduce this manual data entry, you can use tools to configure many models by default. You can copy settings between models, enabling each model to later change the settings independently of each other. Alternatively, you can share settings, enabling you to change the settings for multiple models simultaneously with minimal effort.

Share Common Code Generation Settings among Multiple Models

One way to make models conform to code generation standards is by storing the settings in one location and sharing them among the models. Share settings that you might want to change so that when you do change the settings, the models are updated. To share code generation settings among multiple models, store the settings in a standalone Simulink® data dictionary. Then, link each model to one or more additional, intermediate dictionaries. Configure the intermediate dictionaries to refer to the standalone dictionary. With this modular dictionary hierarchy, each model can store other data, such as design data, separately from the shared settings. For an example that shows how to store shared data in a dictionary, see Partition Data for Model Reference Hierarchy Using Data Dictionaries. Once you set up the data dictionary hierarchy for your models, choose which code generation settings that you want to share by using the dictionary.

Model Configuration Parameter Settings

Configuration parameters control model-wide settings including code generation options such as the system target file, target language, and custom code files. A model's configuration parameters are stored in the configuration set. For code generation parameters on the Code Generation pane and links to the pages for the code generation sub-panes, see Model Configuration Parameters: Code Generation. Some systems that include multiple models require that many of the configuration parameter settings be consistent across the models. For example, when you generate code for a model reference hierarchy, the models in the hierarchy must have the same settings for many of the code generation parameters. In this type of system, share one configuration set with the models.

To share configuration settings among multiple models, store the settings in a standalone configuration set object. Save the configuration object in a Simulink data dictionary that the models share. For each model, activate a configuration reference that points to the shared configuration set. For an example of how to share a configuration set among multiple models, see Share a Configuration with Multiple Models. When you want to change parameter settings, edit the shared configuration set. The models that reference it use the new settings. To quickly share a configuration set among referenced models in a hierarchy, see Share a Configuration Across Referenced Models.

Code Interface Definitions

For the code interface of many models to conform to the same specifications, define the interface in a set of shared code interface definitions in an Embedded Coder Dictionary. Use a code definition to specify many detailed code properties. Then you can control the code interface of your data items and functions by applying the code definitions using the code mapping. Code interface definitions include:

  • Storage classes

  • Memory sections

  • Function customization templates

Once you create your code interface definitions, apply them to a model by mapping model data and functions to the definitions. Sharing one set of code definitions among multiple models enables you to quickly apply the same set of interface specifications by mapping the data and functions in each model to the definitions.

To share a set of code definitions among multiple models, create the definitions in the Embedded Coder Dictionary section of the Simulink data dictionary that the models share. Then, map the data and functions in the model to the shared definitions. For an example of how to share definitions among multiple models, see Share Embedded Coder Dictionary Definition Between Models.

Code Mappings

Models can also share the default mapping configuration that applies the definitions to data and function categories. In the shared Embedded Coder Dictionary, you configure the dictionary default mapping. When you link a model to the dictionary, the model uses the dictionary mapping by default. You can still change the default mapping and individual mapping for an individual model if the model requires a different configuration. For more information about sharing the dictionary default mapping among multiple models, see Configure Default Code Mapping in a Shared Dictionary.

Copy Code Generation Settings to New Models

If you do not want to share code generation settings from one source, you can use other tools to configure models by copying the code generation settings. When you use these techniques, you can easily change the settings for an individual model after the initial configuration. If you want to change a setting for many models that are already configured, you must change each model individually. In the model development process, consider when you want to apply the settings to your models and use one of these techniques.

Template Models

If you want new models to be configured at the time they are created, use a template model. You can create a template model that has preconfigured settings such as configuration parameter settings and Code Mappings editor settings. To create a model, you and others can instantiate the template. Each new model can later change their settings independently of other models. See Create Template from Model.

When you create a model by instantiating a template, the new model is not linked to a data dictionary. If the preconfigured settings in the template model depend on shared data that you store in a data dictionary, you must configure dictionary linking and referencing for each new model.

Template Projects

If you want new models to use data that you store outside of the models, such as in a data dictionary, use template projects. You can create a template project that contains models, data dictionaries, and other artifacts. You can also write project shortcuts that execute custom code to create models, copy settings, and make shared settings stored in a dictionary available to new models. For more information about using template projects, see Create Templates for Standard Project Settings.

Programmatic Configurations

If you want to copy settings to a model after you create it, you can write scripts, callbacks, and shortcuts that use the programmatic interfaces to configure code generation settings. Configure these code generation settings by using the corresponding programmatic interfaces:

Choose how to execute the code by considering when you want to configure the models. You can execute this code in:

  • A script. You can configure the models at any time by running the script manually.

  • A model callback. A callback uses your code to configure the model each time the callback is called. For example, you can use the PreLoadFcn callback, which executes whenever you open a model. See Model Callbacks.

  • A project shortcut. The shortcut can create a model and copy settings into the model. See What Can You Do With Project Shortcuts?.

After you use the code to configure the models, you can change the settings on each individual model as needed.

Related Topics