Log Signals to File

Signal Logging to a File

Logging signals to a file saves data to a variable in your MATLAB® workspace and then saves that data to a MAT-file. This allows you to use MATLAB functions for data analysis and MATLAB plotting functions for visualization.

If your model contains Outport blocks, you cannot save signal data in Simulink® external mode. Simulink supports signal logging to a file 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 for data archiving.

Set Scope Parameters for Logging to File

You save data to a file by first saving the data to the MATLAB workspace through a Simulink Scope block. You need to set Scope block parameters 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 a file. 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 & Triggering dialog box and the Data Archiving 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, you can 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 & Triggering dialog box. The smaller of either value limits the number of sample points saved to the MATLAB workspace. When using Real-Time Windows Target™ software, you should use the Duration value to set the number of sample points you save. To set the Duration value, see Set External Mode Signal and Trigger Properties.

  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.

    The Scope parameters dialog box is related to the Data Archiving dialog box. In the Scope parameters dialog box, you must select the Save data to workspace check box to be able to save data to a file, for two reasons:

    • The data is first transferred from the data buffer to the MATLAB workspace before being written to a MAT-file.

    • The Variable name entered in the Scope parameters dialog box is the same as the one in the MATLAB workspace and the MAT-file.

    If you do not select the Save data to workspace check box, the MAT-files for data logging will be created, but they will be empty.

  6. From the Format list, choose either Structure with time, Structure, or Array (compatible with Version 2.0 through Version 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.

    You must rebuild your real-time application before connecting and starting the application with changed settings. If you do not rebuild after making changes, an error will occur.

Set External Mode Signal and Trigger Properties

Data is saved to a file by first saving the data to the MATLAB workspace through a Simulink Scope block. Signal and triggering properties need to be set when running a real-time application.

After you create a Simulink model and add a Scope block, you can enter the signal and triggering properties for data logging to a file. 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, then 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, and use the Duration value to set the number of sample points saved.

    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 you need to collect for a run. If you enter a value less than the total number of sample points, you may lose sample points due to the time spent transferring values from the data buffer to the MATLAB workspace.

    You should also 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, but you do not have to rebuild your real-time application.

Set External Mode Data Archiving Parameters

The Data Archiving dialog box is related to the Scope parameters dialog box. In the Scope parameters dialog box, you must select the Save data to workspace check box to be able to save data to a file, for two reasons:

  • The data is first transferred from the scope data buffer to the MATLAB workspace before being written to a MAT-file.

  • The Variable name entered in the Scope parameters dialog box is the same as the one in the MATLAB workspace and the MAT-file. Enabling the data to be saved enables a variable named with the Variable name parameter to be saved to a MAT-file.

If you do not select the Save data to workspace check box in the Scope parameters dialog box, the MAT-files for data logging will be created, but they will be empty.

After you create a Simulink model, you can enter the Data Archiving Parameters for data logging to a file:

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

    The External Mode Control Panel dialog box opens.

  2. Click the Data Archiving button.

    The External Data Archiving dialog box opens. This dialog box allows you to specify data archiving options.

  3. Select the Enable archiving check box.

  4. In the Directory text box, enter the path to a folder on your disk. For example, if your MATLAB working folder is named mwd, enter

    c:\mwd
    
  5. In the File text box, enter the file name prefix for the data files to be saved. For example, enter

    data
    

    The MATLAB environment names the files data_0.mat, data_1.mat, etc. The number of files equals the total sample points. For example, if you set Duration to Total sample points, then only one file is created.

  6. Select the Append file suffix to variable names check box.

    Within each MAT-file, a variable is saved with the same name you entered in the Variable name text box (History pane on the Scope parameters dialog box). By selecting the Append file suffix to variable names check box, the same suffix that is added to the MAT-file is added to the variable name. For example, if you entered the variable name ScopeData, then within the file data_0.mat will be a variable ScopeData_0.

    Your External Data Archiving dialog box looks like this:

  7. Click the Close button.

    The parameters you entered are applied to your model.

    There is no Apply button with this dialog box. You must click the Close button for the changes you make to take effect.

Plot Signal Data Logged to File

You can use the MATLAB plotting functions for visualization of your non-real-time simulated data or your real-time executed data.

After running your real-time application and logging data to a file, 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 the fields time vector, signal structure, and a string containing the block name.

    ScopeData = 
             time: [10000x1 double]
          signals: [1x1 struct]
        blockName: 'rtwin_model/Scope'
    
  2. To list the MAT-files saved to your disk, type

    dir *.mat
    

    The MATLAB Command Window shows the MAT-files in your current working folder.

    ScopeData.mat
    
  3. To clear the MATLAB workspace and load the scope data, type

    clear 
    load ScopeData 
    who
    

    The MATLAB Command Window displays:

    Your variables are:
    ScopeData
    
  4. To plot the first 1000 points, type

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

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

Was this topic helpful?