Provide Signal Data for Simulation
A Simulink® model performs algorithms on input signal data and produces output signals. The model defines what input data to use at the start of simulation and what output to capture at the end of simulation. As you create and simulate your model, you:
As you create, debug, and test a model, you can use different sets of input signal data for simulation. You can use logged simulation data as input to another simulation.
Identify Model Signal Data Requirements
To use system-generated signal data, use source blocks such as a Sine Wave block. Source blocks do not require the use of a variable or external data source. If you cannot configure source blocks to meet your modeling requirements, then supply the signal data.
As you determine your signal data requirements, identify the:
Blocks (including subsystems and Model blocks) that you need to provide data for — Design interfaces for blocks and for model components, including data types of signals.
Range characteristics of signals, such as sample time, dimensions, and data type.
Storage location for data for each input signal — Determine where to store signal data: in workspace variables, a MAT-file, or an external data file such as an Microsoft® Excel® spreadsheet.
Create a list of equation variables and constant coefficients, and then determine the coefficient values from published sources or by performing experiments on the system.
For information about storage locations for signal data, see Signal Data Storage for Loading.
Signal Data Storage for Loading
Store signal data for loading into a model in these locations:
MATLAB® (base) workspace, or function workspace
Model workspace
Function workspace
Masking workspace
Blocks
MAT-files
Spreadsheets
The MATLAB (base) workspace is the most common workspace to use for loading signal data.
MATLAB Workspace for Signal Data
Consider using the MATLAB (base) workspace when you want to:
Use a small amount of signal data for iterative simulations.
Use signal data logged during one simulation as input for another simulation.
Have multiple models use the same signal data.
Create Signal Data in MATLAB Workspace
At the MATLAB command line or editor, create the signal data.
Use the
xlsread
function to read data from an Excel spreadsheet into the MATLAB workspace.Use the
readmatrix
function to read data from a CSV spreadsheet into the MATLAB workspace.Use a model callback to load signal data.
Use a Simulink logging techniques such as:
Mark signals for logging.
Log output data.
Connect a signal to the To Workspace block.
Connect one or more signals to the Record block.
Connect one or more signals to a Scope block and configure the block to log data to the workspace.
Enable Data stores logging and add a Data Store Memory block to the model to log data stores.
Enable States logging or Final states logging to log state data.
Use the
sim
command configured to log simulation data.
Load Signal Data from MATLAB Workspace. To load signal data from a workspace, use one of these techniques:
Add a From Workspace block to the model.
Add a Playback block to the model and add data from the workspace.
Use a root-level input port.
Specify workspace variables in the Configuration Parameters > Data Import/Export > Input parameter.
Use the Root Inport Mapper tool to specify the data for the Input parameter.
Source and Signal Editor Blocks for Signal Data
Source blocks, such as the Sine Wave block, generate signals that you can use as inputs to other blocks. Source blocks do not store signal data. Source blocks can be useful for initial prototyping of a model when the generated signal data serves your modeling requirements.
To define scenarios to use as inputs to a model, you can use the Signal Editor block. The Signal Editor block stores the scenario definitions.
Consider using a source block to:
Avoid having to create the data manually.
Reduce memory consumption — source blocks do not store signal data.
Graphically represent in the model the kind of signal data.
Consider using a Signal Editor block to:
Create and import scenarios for use in testing.
You can use scenarios with Simulink and with these products:
Simulink Test™
Simulink Coverage™
Simulink Design Verifier™
Switch between scenarios quickly.
MAT-Files for Signal Data
Consider storing signal data in a MAT-file to:
Load a large amount of signal data efficiently.
Reuse the same signal data in different models.
Reduce memory requirements for the model.
Use different sets of signal data with the same model, with minimal model updates.
Store Signal Data in MAT-File. To create a MAT-file to store signal data to import, you can use:
A To File block
A Record block
The Signal Editor user interface
MATLAB to create signal data that you store in a MAT-file
Load Signal Data from MAT-File. To load signal data from a MAT-file into a model, you can use a From File block or a Playback block.
Spreadsheets for Signal Data
Consider using an Excel or CSV spreadsheet to:
Use an existing spreadsheet that already has the necessary signal data or that you can update easily to contain the signal data.
Load a large amount of signal data efficiently.
Reduce memory requirements for the model.
Use different sets of signal data with the same model, with minimal model updates.
Share the signal data with other people who do not have Simulink installed.
Store Signal Data in Spreadsheet. To store signal data in a spreadsheet, you can use one of these approaches:
Load Signal Data from Spreadsheet. Use the From Spreadsheet block or the Playback block to load data from a spreadsheet.
The From Spreadsheet block loads Microsoft Excel on all platforms. This block loads CSV spreadsheets only on Microsoft Windows® platforms.
The From Spreadsheet block incrementally loads the data directly from the spreadsheet, to reduce memory consumption.
Load Input Signal Data
You can use various sources for input signal data for simulating a model. You can:
Use existing data from a file, such as a spreadsheet.
Write a MATLAB script to define variables for the signal data. For example, you can create
Dataset
format data that you can use with all the signal loading techniques.Use data logged from a previous simulation.
You can use several different approaches to load data into a model, including:
Root-level input ports — Import signal data from a workspace, using the Input configuration parameter to import it to a root-level input port of a Inport, Enable, or Trigger block. You can specify the input data directly in the Input parameter. To import multiple signals to root-level input ports, consider using the Root Inport Mapping tool. That tool updates the Input parameter based on the signal data that you import and map to root-level input ports.
Source blocks — Add a source block, such Sine Wave block, to generate signals to input to another block.
Playback block — Load data from a MAT-file or a Microsoft Excel file.
From File block — Read data from a MAT-file, outputting the data as a signal.
From Spreadsheet block — Read data from Microsoft Excel spreadsheets or CSV spreadsheets, outputting the data as one or more signals.
To determine the approach to meet the input signal data requirements of your model, see Comparison of Signal Loading Techniques.
Log Output Signal Data
You can save signal values to the MATLAB workspace or to a MAT-file during simulation for later retrieval and postprocessing. Saving simulation data is also known as logging or exporting simulation data.
To determine which approach to use for logging signal data, see Save Simulation Data.
Saving simulation data in Dataset
format simplifies postprocessing by
providing a common format for the results of various logging techniques. Using
Dataset
format stores the data as MATLAB
timeseries
objects, which you can process with MATLAB. Simulink provides tools for converting data logged in other formats to
Dataset
format.
For more information about logging output signal data, see Save Run-Time Data from Simulation.