Main Content

Link MAT File to Model as External Data Source

In the context of a Simulink® model, an external data source is any file that stores data, variables, objects, and interfaces outside of the model itself. External data sources include the base workspace, data dictionaries, and MAT files. Unlike model workspace data, which is local to the model, data from these other sources are external to the model. For more information, see Types of Data Storage.

MAT files and data dictionaries provide permanent data storage. When you link a MAT file to the model as an external data source, the data is permanently stored with the model. You can associate a single MAT file to multiple models to share data between them, or link multiple MAT files to a single model.

Tip

If you use the base workspace to create variables, save the data in a MAT file and link it to your model as an external data source to create permanent data storage. This avoids the need to load the MAT file in each MATLAB® session. To save base workspace data, see Save and Load Parts of Variables in MAT-Files.

To associate a MAT file as an external data source to the model:

  1. Open your Simulink model. In the Simulink Editor, on the Modeling tab, click Model Explorer.

  2. In the Model Properties dialog box, navigate to the External Data tab

  3. In the Additional data sources section, click Browse to locate and open your MAT file.

  4. Click OK to link the MAT file.

Using MAT file as External Data Source in Simulink Model

This example shows how to use a MAT file as an external data source to manage the data for the Simulink model ModelingWithData.

Open the model ModelingWithData.

open_system('ModelingWithData');

The data for the model is defined in the MAT file ModelingWithDataDefinitions.mat and is linked to the model as an external data source. The link is set up on the External Data tab of the Model Properties dialog box. To open this dialog box, click the icon in the lower-left corner of the model window and select the gear icon on the right.

To open the data in the Model Explorer, in the model window, click the same icon and select External Data. In the Model Explorer, select ModelingWithDataDefinitions.mat under the External Data node. In the Contents pane, you can see the variables that are used in the model for simulation.

Limitations

MAT files do not support the storage of:

  • Enumerated members

  • Simulink.ConfigSet objects

  • Simulink.ConfigSetRef objects

  • Simulink.VariantBank objects

  • Simulink.VariantBankCoderInfo objects

  • Simulink.VariantConfigurationData objects

  • timeseries objects

Manage External Data Sources Programmatically

You can link one data dictionary and multiple MAT files as external data sources in a model. You can manage the external data sources programmatically by using these functions:

FunctionPurpose
Simulink.data.dataSource.addSourceAdd external data source to model
Simulink.data.dataSource.removeSourceRemove external data source from model
Simulink.data.dataSource.hasSourceDetermine if specified external data source is linked to model
Simulink.data.dataSource.getSourceNamesGet list of all external data sources linked to model

Related Topics