Products & Services Solutions Academia Support User Community Company

Learn more about Simulink   

Logging Signals

About Signal Logging

Logging signals refers to the process of saving signal values to the MATLAB workspace during simulation for later retrieval and postprocessing. Simulink allows you to log a signal by

All of these methods allow you to specify the names of the workspace variables used to save signal data and to limit the amount of data logged for a particular signal.

See Simulink Reference for the To Workspace and Outport blocks for information on using these blocks to log signal data. See the documentation of the sim command for some data logging capabilities that are available only for programmatic simulation.

Globally Enabling and Disabling Logging

You can globally enable or disable signal logging for a model by checking or unchecking the Signal logging option on the Data Import/Export pane of the Configuration Parameters dialog box (see Signal logging ). Simulink logs signals only if this option is checked. If the option is not checked, Simulink ignores the signal logging settings for individual signals.

Enabling Logging for a Signal

To enable signal logging for a signal, select the Log signal data option on the signal's Signal Properties dialog box. See Signal Properties Dialog Box for more information.

Enabling Signal Logging Programmatically

You can enable signal logging programmatically for selected blocks with the outport DataLogging property. You can set this property using the set_param command. For example:

  1. At the MATLAB Command Window, open a model. Type

    vdp
  2. Select a block in that model. For example, select the Mux block.

  3. Get the port handles of the selected block.

    get_param(gcb,'PortHandles')
  4. Enable signal logging for the desired outport port.

    set_param(ans.Outport(1),'DataLogging','on')

    The logged signal indicator ( ) appears.

Displaying Logging Indicators

By default, Simulink displays an indicator on each signal whose Signal Properties > Log signal data option is enabled. For example, in the following model the output of the Sine Wave block is logged:

A logged signal can also be a test point. See Working with Test Points for information about test points.

To turn display of logging indicators on or off, select or clear Port/Signal Displays > Testpoint/Logging Indicators from the Simulink Format menu.

Specifying a Logging Name

You can assign a name, called the logging name, to the object used to log data for a signal during simulation. To specify a log name for a signal, select Custom from the Logging name list on the signal's Signal Properties dialog box and enter the custom name in the adjacent text field.

If you do not specify a custom logging name, Simulink uses the signal name, or if there is no name, Simulink generates a default name that is composed of the block name and port number. For example, if the block name is MyBlock and the signal being logged is the first output of this block, Simulink generates the following name: SL_MyBlock1.

Limiting the Data Logged for a Signal

The Data panel of the Signal Properties dialog box lets you limit the amount of data logged for a signal. For example, you can specify the maximum amount of data to be logged for a signal or a decimation factor that causes Simulink to skip a specified number of time steps before logging a signal value. See Data for more information.

Logging Virtual Signals

A virtual signal is a signal that graphically represents other signals or parts of other signals. Virtual signals are purely graphical entities; they have no functional or mathematical significance. The source of a virtual signal is always a virtual block, like a Mux block or Selector block. See Virtual Signals and Mux Signals for more information.

The nonvirtual components of a virtual signal are called regions. A virtual signal can contain the same region more than once. For example, if the same nonvirtual signal is connected to two input ports of a Mux block, the block outputs a virtual signal that has two regions.

The log of a virtual signal that contains duplicate regions includes all of the regions, even though the data in each is the same. Logged virtual signal regions appear in the log in a Simulink.TsArray object. The log gives the duplicate regions unique names, using the syntax: <signal_name>_reg<#counter>.

Logging Multidimensional Signals

You can log multidimensional signals, which are signals whose elements are nonscalar. The techniques for logging multidimensional signals are the same as those for logging any other signal. See Signal Dimensions for information about multidimensional signals.

Logging Composite Signals

You can log Simulink composite signals, which are called buses. The hierarchy of a bus signal is preserved in the logsout object. The logged name of a signal in a virtual bus derives from the name of the source signal. The logged name of a signal in a nonvirtual bus derives from the applicable bus object, and can differ from that of the source signal. See Using Composite Signals and Using Bus Objects for information about those capabilities.

Logging Referenced Model Signals

You can log any signal that is defined as a test point in a referenced model. For information about test points, see Designating a Signal as a Test Point. For information about referenced models, see Referencing a Model.

To log test pointed signals in referenced models, select the Model block and then select Log referenced signals from the model editor's Edit menu or from the block's context menu.

The Model Reference Signal Logging dialog box appears.

The dialog box contains the following panes and controls.

Model Hierarchy

This pane displays the contents of the referenced model as a tree control with expandable nodes. The top-level node represents the referenced model. Expanding this node displays the subsystems that the referenced model contains and any models that it itself references. Expanding a subsystem node displays the subsystems that it contains and the models that it references.

Refresh Button

Refreshes the dialog box to reflect changes in the model hierarchy.

Signals

This pane displays the test points of the model or subsystem selected in the Model Hierarchy pane. See Working with Test Points. Check the check box next to a test point's name to specify that it should be logged.

Log signals as specified by the referenced model

Checking this check box causes Simulink to log the signals that the referenced model specifies should be logged.

Signal Properties

This pane is enabled if Log signals as specified by the referenced model is not selected. In this case, the controls on this pane allow you to specify the signal logging properties of the signal selected in the Signals pane. The values that you specify override for this instance of the referenced model those specified by the model itself. The controls correspond to the controls of the same name on the Signal Properties dialog box. See Signal Properties Dialog Box for information on how to use them.

Viewing Logged Signal Data

To view logged signal data, either check Configuration Parameters > Data Import/Export > Inspect signals when simulation is stopped/paused or select Tools > Inspect Logged Signals from the model editor's menu bar. The first method causes Simulink to display logged signals in the MATLAB Time Series Tools viewer (see Time Series Tools) whenever a simulation ends or you pause a simulation. The second method causes Simulink to display the data immediately.

Accessing Logged Signal Data

Simulink saves signal data that it logs during simulation in a Simulink data object of type Simulink.ModelDataLogs that resides in the MATLAB workspace. The name of the object's handle is logsout by default. The Data Import/Export configuration pane (see Data Import/Export Pane) allows you to specify another name for this object. See Simulink.ModelDataLogs for information on extracting signal data from this object. The signal logs for particular model elements are contained in the objects in the following table.

Model ElementSignal Data Object
Top-level or referenced modelSimulink.ModelDataLogs
Subsystem in a modelSimulink.SubsysDataLogs
Scope block in a modelSimulink.ScopeDataLogs
Signal other than a mux or busSimulink.Timeseries
Mux or bus signalSimulink.TsArray

Handling Spaces and Newlines in Logged Names

Names that include space or newline characters can improve the readability of a block diagram, but referencing names that include such characters in a data log requires special techniques. These techniques allow the MATLAB parser to process the names even though spaces and newlines are not legal in MATLAB identifiers. Signal names in data logs can have spaces or newlines in their names under the following circumstances:

The following three examples show a signal whose name contains a space, a signal whose name contains a newline, and an unnamed signal that originates in a block whose name contains a newline:

If you execute these examples with data logging enabled in the Data Import/Export pane, accepting the default logging object name logsout, and then type logsout in the MATLAB Command Window, MATLAB displays the following data log:

logsout =
 
Simulink.ModelDataLogs (model_name):
  Name                   Elements  Simulink Class

  ('x y')                   1      Timeseries
  ('a
b')                   1      Timeseries
  ('SL_Sine
Wave1')         1      Timeseries

You cannot access any of the Timeseries objects in this log using TAB name completion, or by typing the name to MATLAB, because the space or newline in each name appears to the MATLAB parser as a separator between identifiers. For example:

>> logsout.x y

??? logsout.x y
              |
Error: Unexpected MATLAB expression.

To reference a Timeseries object whose name contains a space, as in the first example above, single-quote the element containing the space:

>> logsout.('x y')

          Name: 'x y'
     BlockPath: 'model_name/Sine'
     PortIndex: 1
    SignalName: 'x y'
    ParentName: 'x y'
      TimeInfo: [1x1 Simulink.TimeInfo]
          Time: [51x1 double]
          Data: [51x1 double]

To reference a Timeseries object whose name contains a newline, as in the second example above, concatenate to construct the element containing the newline:

>> cr=sprintf('\n')
>> logsout.(['a' cr 'b'])

The same techniques work when a space or newline in a data log derives from the name of:

This code can reference logged data for the signal in the third example above:

>> logsout.(['SL_Sine' cr 'Wave1'])

For names with multiple spaces, newlines, or both, repeat and combine the two techniques as needed to specify the intended name to MATLAB. No analogous techniques exist for TAB name completion, which never works with names that contain space or newline characters.

Extracting Partial Data from a Running Simulation

Before a simulation ends, you can extract and write the currently logged signal data from Simulink.ModelDataLogs with the set_param WriteDataLogs command. The currently logged signal is the partial data logged between when the simulation started and when you request an extraction of the signal data. If you use this command during the simulation, Simulink writes the current logging variable values to the MATLAB workspace. If you use this command at the end of the simulation, Simulink writes the values from the last simulation to the MATLAB workspace.

To use this command, type the following at the MATLAB Command Window.

set_param(bdroot,'SimulationCommand','WriteDataLogs')

Example: Logging Signal Data in the F14 Model

Enabling signal logging on a signal-by-signal basis allows you to store signal data without modifying the structure of the Simulink diagram. For example, use the following steps to log and access the signal data for the vertical velocity signal w in the F14 model.

  1. Open the F14 model by typing f14 at the MATLAB command prompt.

  2. Right-click on the signal labeled w and select the Signal Properties menu.

  3. In the Signal Properties dialog box that opens, select the Log signal data option. The logging name initializes to the signal's name.

  4. Click the OK button on the Signal Properties dialog box. The 'blue antenna' icon appears on the signal labeled w, indicating that this signal will be logged during simulation.

  5. Ensure that Configuration Parameters > Data Import/Export Signal logging is selected and that the logging name is set to the default variable logsout.

  6. Run the F14 simulation. The logged signal data is stored in a Simulink.ModelDataLogs object named logsout in the MATLAB workspace. Typing logsout at the MATLAB command prompt displays the following

    logsout =
     
    Simulink.ModelDataLogs (f14):
      Name                   Elements  Simulink Class
    
      w                         1      Timeseries
  7. Type logsout.w to view the information stored for the signal w.

    logsout.w
              Name: 'w'
         BlockPath: 'f14/Aircraft Dynamics Model'
         PortIndex: 1
        SignalName: 'w'
        ParentName: 'w'
          TimeInfo: [1x1 Simulink.TimeInfo]
              Time: [1353x1 double]
              Data: [1353x1 double]
  8. To inspect the signal using the MATLAB Time Series Tools, select Inspect Logged Signals from the f14 model editor's Tools menu (see Time Series Tools in the MATLAB documentation).

Signal Logging Limitations

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS