Log Signals to MATLAB Workspace

Signal Logging to the Workspace

Logging signals to the workspace saves data to a variable in your MATLAB® base workspace. This process allows you to use MATLAB functions for data analysis and MATLAB plotting functions for visualization. You can save data to a variable during a simulation or during an execution.

If your model contains Outport blocks, you cannot save signal data in Simulink® external mode. Simulink supports signal logging with Outport blocks in normal mode only, when Simulink runs the simulation algorithm.

    Tip   In external mode, do not enter or select parameters on the Data I/O tab in the Configuration Parameters dialog box. Instead, add a Scope block to your Simulink model and use it to log signal data.

Set Scope Parameters for Logging to Workspace

Data is saved to the MATLAB workspace through a Simulink Scope block. Scope block parameters need to be set for data to be saved. After you create a Simulink model and add a Scope block, you can enter the scope parameters for signal logging to the MATLAB workspace. This procedure uses the Simulink model rtwin_model as an example, and assumes you have already loaded that model.

    Note:   If you entered the scope parameters for running a simulation, you may want to look over this procedure, because the Scope parameters dialog box is related to the External Signal and Triggering dialog box.

  1. In the Simulink window, double-click the Scope block.

    A Scope window opens.

  2. Click the Parameters button   on the toolbar.

    A Scope Parameters dialog box opens.

  3. Click the History tab.

  4. Do one of the following:

    • If you are running a simulation, select the Limit data points to last check box, and enter the number of sample points to save.

    • If you are running an execution, do not select the Limit data points to last check box.

    The Limit data points to last check box is related to the Duration value in the External Signal and Triggering dialog box. The smaller of either value limits the number of sample points saved to the MATLAB workspace. When you are using Real-Time Windows Target™ software, use the Duration value to set the number of sample points you save. To set the Duration value, see the next section.

  5. Select the Save data to workspace check box. In the Variable name text box, enter the name of a MATLAB variable. The default name is ScopeData.

  6. From the Format list, choose Structure with time, Structure, or Array (compatible with V2.0-2.2). For example, to save the sample times and signal values at those times, choose Structure with time.

    Your History pane looks like this:

  7. Do one of the following:

    • Click Apply to apply the changes to your model and leave the dialog box open.

    • Click OK to apply the changes to your model and close the dialog box.

    When you modify a value in the Scope parameters dialog box, you must click the Apply or OK button for the changes to take effect, and you must rebuild your real-time application before connecting and starting it. If you do not rebuild, an error dialog box will open. If you do not click Apply, your executable will run, but it will use the old settings.

    The reason why you need to rebuild is because the model checksum includes settings from the Scope block used for signal logging. If the model checksum does not match the checksum in the generated code, the real-time application cannot run. Rebuild your real-time application after changing Scope parameters.

Set External Mode Signal and Trigger Properties

Data is saved to the MATLAB workspace through a Simulink Scope block. Signal and triggering properties need to be set only when you are running a real-time application. If you are running a normal mode simulation, you can skip this procedure.

After you create a Simulink model and add a Scope block, you can enter the signal and triggering properties for logging to the MATLAB workspace. This procedure uses the Simulink model rtwin_model as an example and assumes you have already loaded that model:

  1. In the Simulation window, click Code > External Mode Control Panel.

    The External Mode Control Panel dialog box opens.

  2. Click the Signal & Triggering button.

    The External Signal & Triggering dialog box opens.

  3. Click the Select all button. From the Source list, choose manual. From the Mode list, choose normal.

    The X under Signal selection designates that a signal has been tagged for data collection, and T designates that the signal has been tagged as a trigger signal.

  4. In the Duration field, enter the number of sample points in a data buffer. For example, if you have a sample rate of 1000 samples/second and a stop time of 10 seconds, you could enter

    10000
    

    The Duration value is related to the Limit data points to last value in the Scope parameters dialog box. The smaller of either value limits the number of sample points saved to the MATLAB workspace. You should not select the Limit data points to last check box; instead, use the Duration value to set the number of sample points saved. To clear the Limit data points to last check box, see Set Scope Parameters for Logging to Workspace.

    The Duration value specifies the number of contiguous points of data to be collected in each buffer of data. You should enter a Duration value equal to the total number of sample points that you need to collect rather than relying on a series of buffers to be continuous.

    If you enter a value less than the total number of sample points, you may lose sample points during the time spent transferring values from the data buffer to the MATLAB workspace. The Real-Time Windows Target software maintains point continuity only within one buffer. Between buffers, because of transfer time, some samples will be omitted.

    Also, you should set the time axis for Simulink Scope blocks equal to the sample interval (in seconds) times the number of points in each data buffer. This setting will display one buffer of data across the entire Simulink Scope plot.

    The External Signal & Triggering dialog box looks like this:

  5. Do one of the following:

    • Click Apply to apply the changes to your model and leave the dialog box open.

    • Click Close to apply the changes to your model and close the dialog box.

    You must click the Apply or Close button on the External Signal & Triggering dialog box for the changes you made to take effect.

Plot Signal Data Logged to Workspace

You can use the MATLAB plotting functions for visualizing non-real-time simulated data or real-time application data.

After running your real-time application and logging data to the MATLAB workspace, you can plot the data. This procedure uses the Simulink model rtwin_model as an example, and assumes you saved your data to the variable ScopeData.

  1. In the MATLAB Command Window, type

    ScopeData
    

    The MATLAB Command Window shows the structure of the variable ScopeData. The variable ScopeData is a MATLAB structure containing fields for the time vector, signal structure, and a string containing the block name.

    ScopeData = 
                 time: [10000x1 double]
              signals: [1x1 struct]
            blockName: 'rtwin_model/Scope'
    

    To list the contents of the structure signals, type

    ScopeData.signals
    

    The MATLAB Command Window shows the structure of the variable ScopeData.signals. This structure contains one or more vectors of signal data depending on the number of signal inputs to the Scope block.

    ans = 
           values: [10000x1 double]
       dimensions: 1
            label: ''
            title: []
        plotStyle: 1
    
  2. To plot the first 1000 points, type

    plot(ScopeData.time(1:1000),ScopeData.signals.values(1:1000))
    

    The MATLAB environment plots the first 1000 samples from 0.0000 to 0.9990 second.

  3. The variable ScopeData is not automatically saved to your hard disk. To save the variable ScopeData, type

    save ScopeData

    The MATLAB environment saves the scope data to the file ScopeData.mat.

Was this topic helpful?