This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Convert Logged Data to Dataset Format

Convert Workspace Data to Dataset

This example shows how to convert MATLAB® time-series data to Dataset format. myvdp_timeseries is the vdp model with two To Workspace blocks configured for simout and simout1 logging data in MATLAB time-series format. Consider using a procedure like this one if you have models that use To Workspace blocks to log data to MATLAB time-series format.

Use the Simulink.SimulationData.Dataset constructor to convert the MATLAB time-series data to Dataset format and then concatenate the two data sets.

  1. Starting with the vdp model, add two To Workspace blocks to the model as shown.

  2. Set the Save format parameter of both blocks. Set Timeseries.

  3. Save the model as myvdp_timeseries.

  4. Simulate the model.

    The simulation logs data using the To Workspace blocks.

  5. Access the signal logging format, logsout.

    logsout
    
    
    logsout = 
    
      Simulink.SimulationData.Dataset
      Package: Simulink.SimulationData
    
      Characteristics:
                  Name: 'logsout'
        Total Elements: 2
    
      Elements:
        1: 'x1'
        2: 'x2'
    
      -Use get or getElement to access elements by index or name.
      -Use addElement or setElement to add or modify elements.
    
      Methods, Superclasses
  6. Convert the MATLAB time-series data from both To Workspace blocks to Dataset.

    ds = Simulink.SimulationData.Dataset(simout);
    ds1 = Simulink.SimulationData.Dataset(simout1);

    ds is the variable name of the first To Workspace block data. ds1 is the variable name of the second To Workspace block data.

  7. Concatenate both datasets into dsfinal. Observe that the format of dsfinal matches that of logsout.

    dsfinal = ds.concat(ds1)
    dsfinal = 
    
      Simulink.SimulationData.Dataset
      Package: Simulink.SimulationData
    
      Characteristics:
                  Name: 'simout'
        Total Elements: 2
    
      Elements:
        1: 'x1'
        2: 'x2'
    
      -Use get or getElement to access elements by index or name.
      -Use addElement or setElement to add or modify elements.
    
      Methods, Superclasses

Convert Structure Without Time to Dataset

This example shows how to convert structure without time data to Dataset format. myvdp_structure is the vdp model with two To Workspace blocks configured for simout and simout1 logging data in structure format, as shown.

If you have models that use To Workspace blocks to log data to structure format, consider using a procedure like this one to convert them to Dataset format.

  1. Starting with the vdp model, add two To Workspace blocks to the model as shown.

  2. In the Save format parameter of both blocks, select Structure.

  3. Enable signal logging for the two signals going to the two To Workspace blocks to log in Ds format.

  4. Save the model as myvdp_structure.

  5. Simulate the model.

    The simulation logs data using the To Workspace blocks.

  6. Convert the structure data from both To Workspace blocks to Dataset.

    ds = Simulink.SimulationData.Dataset(simout);
    ds1 = Simulink.SimulationData.Dataset(simout1);

    simout is the variable name of the first To Workspace block data. simout1 is the variable name of the second To Workspace block data.

    With the conversion of structure without time or an array, time starts at t=0 and increments by 1.

  7. Get the values of the first element in ds.

    ds.get(1).Values.Time
    ans =
    
         0
         1
         2
         3
         .
         .
         .
        61
        62
        63
  8. Get the time values of the first element from signal logging.

    logsout.get(1).Values.Time
    ans =
    
             0
        0.0001
        0.0006
        0.0031
        .
        .
        .
       19.2802
       19.6802
       20.0000
  9. Observe the discrepancy in timestamps between

    • Data logged in structure without time that you convert to Dataset format

    • Data logged in Dataset format

Programmatically Access Logged Dataset Format Data

When you use the default Dataset signal logging format, Simulink® saves the logging data in a Simulink.SimulationData.Dataset object. For information about extracting signal data from that object, see the Simulink.SimulationData.Dataset reference page.

The Simulink.SimulationData.Dataset object contains a Simulink.SimulationData.Signal object for each logged signal.

For bus signals, the Simulink.SimulationData.Signal object contains a structure of MATLAB timeseries objects.

The Simulink.SimulationData.Dataset class provides two methods for accessing the signal logging data and its associated information.

Name

Description

Simulink.SimulationData.DataSet.get

You can also use the getElement method, which shares syntax and behavior as the get method.

Get element or collection of elements from the dataset, based on index, name, or block path.

Simulink.SimulationData.DataSet.numElements

Get number of elements in the dataset.

For example of accessing signal logging data that uses the Dataset format, see Simulink.SimulationData.Dataset.

Access Array of Buses Signal Logging Data

Signal logging data for an array of buses uses Dataset signal logging format.

The general approach to access data for a specific signal in an array of buses is:

  1. Use a Simulink.SimulationData.Dataset.get (or getElement) method to access a specific signal in the logged data (by default, the logsout variable).

  2. To get the values, index within the array of buses.

  3. Index again to get data for a specific bus.

For example, to obtain the signal logging data for the Constant6 block in the ex_log_nested_aob model, for the topBus signal that feeds the Terminator block:

logsout.getElement('topBus').Values.a(2,2).firstConst.data

Here are additional examples of accessing array of buses signal logging data. For another example that shows how to log array of buses data, see sldemo_mdlref_bus.

 Simple Array of Buses

 Array of Buses in a Bus

 Nested Arrays of Buses

Accessing Data for Signals with a Duplicate Name

For a model with multiple signals that have the same signal name, signal logging data includes a Simulink.SimulationData.Signal object for each signal that has a duplicate name.

To access a specific signal that has a duplicate name, use one of these approaches:

  • To find the data for the specific signal, visually inspect the displayed output of Simulink.SimulationData.Signal objects.

  • Use the Simulink.SimulationData.Dataset.getElement method, specifying the blockpath for the source block of the signal.

  • To iterate through the signals with a duplicate signal name, create a script using the Simulink.SimulationData.Dataset.getElement method with an index argument.

  • Use the Signal Properties dialog box to specify a different name. Consider using this approach when the signals with a duplicate name do not appear in multiple instances of a referenced model in normal mode.

    1. In the model, right-click the signal.

    2. In the context menu, select Properties.

    3. In the Signal Properties dialog box, set Logging name to Custom and specify a different name than the signal name.

    4. Simulate the model and use the Simulink.SimulationData.Dataset.getElement method with a name argument.

Tip

Alternatively, you can use the Signal Logging Selector to access a specific signal. For details, see Override Signal Logging Settings with Signal Logging Selector.

Handling Newline Characters in Signal Logging Data

To handle newline characters in logging names in signal logging data that uses Dataset format, use a sprintf command within a getElement call. For example:

topOut.getElement(sprintf('INCREMENT\nBUS'))

See Also

Related Examples

More About