Main Content

read

Read chunk of data in datastore

Description

example

data = read(dst) reads a chunk of samples from the datastore dst and updates the read position. Subsequent calls to the read function continue reading from the endpoint of the previous call. Use the read function to incrementally process signals that are too large to fit into memory.

  • When the datastore is a matlab.io.datastore.sdidatastore object, the number of samples read by the read function varies. The returned timetable object data always fits into memory.

  • When the data store is a matlab.io.datastore.SimulationDatastore object, use the ReadSize property of the SimulationDatastore object to specify the amount of data, in samples (time steps), to read at a time. Use the progress function and the NumSamples property to determine the current read position.

[data,info] = read(simdst) returns information about the extracted data in info.

Examples

collapse all

A matlab.io.datastore.sdidatastore object references signal data in the Simulation Data Inspector repository. When the signal is too large to fit into memory, you can use the sdidatastore object to incrementally process the data manually or to create a tall timetable for the signal that handles the incremental processing for you.

Create sdidatastore for Signal

Simulate the sldemo_fuelsys model, which is configured to log several signals, to create data in the Simulation Data Inspector repository.

mdl = "sldemo_fuelsys";
sim(mdl);

Logged signal data is returned in a Simulink.SimulationData.Dataset object named sldemo_fuelsys_output.

sldemo_fuelsys_output
sldemo_fuelsys_output = 
Simulink.SimulationData.Dataset 'sldemo_fuelsys_output' with 10 elements

                         Name            BlockPath                                
                         ______________  ________________________________________ 
    1  [1x1 Signal]      ''              sldemo_fuelsys/EGO Fault Switch         
    2  [1x1 Signal]      air_fuel_ratio  sldemo_fuelsys/Engine Gas Dynamics      
    3  [1x1 Signal]      ''              sldemo_fuelsys/Engine Speed Fault Switch
    4  [1x1 Signal]      speed           sldemo_fuelsys/Engine_Speed_Selector    
    5  [1x1 Signal]      ''              sldemo_fuelsys/MAP Fault Switch         
    6  [1x1 Signal]      map             sldemo_fuelsys/MAP_Selector             
    7  [1x1 Signal]      ego             sldemo_fuelsys/O2_Voltage_Selector      
    8  [1x1 Signal]      ''              ...o_fuelsys/Throttle Angle Fault Switch
    9  [1x1 Signal]      throttle        sldemo_fuelsys/Throttle_Angle_Selector  
   10  [1x1 Signal]      fuel            sldemo_fuelsys/To Plant                 

  - Use braces { } to access, modify, or add elements using index.

Use the Simulation Data Inspector programmatic interface to get the signal ID for the signal named speed.

runCount = Simulink.sdi.getRunCount;
latestRunID = Simulink.sdi.getRunIDByIndex(runCount);
latestRun = Simulink.sdi.getRun(latestRunID);
speedSigID = getSignalIDsByName(latestRun,"speed");

Use the signal ID to create an sdidatastore object for the speed signal.

speedSDIds = matlab.io.datastore.sdidatastore(speedSigID);

Verify Contents of Datastore

Check the Name property of the sdidatastore object to verify that it matches your expectations.

speedSDIds.Name
ans = 
'speed'

You can also use the preview function to verify the first ten samples in the signal.

preview(speedSDIds)
ans=10×1 timetable
         Time         Data
    ______________    ____

    0 sec             300 
    0.00056199 sec    300 
    0.0033719 sec     300 
    0.01 sec          300 
    0.02 sec          300 
    0.03 sec          300 
    0.04 sec          300 
    0.05 sec          300 
    0.055328 sec      300 
    0.055328 sec      300 

Process Signal Data with sdidatastore Object

When your signal is too large to fit into memory, you can use the read function to read chunks of data from the Simulation Data Inspector repository to incrementally process your data. Use the hasdata function as the condition for a while loop to incrementally process the whole signal. For example, find the maximum signal value.

latestMax = [];

while hasdata(speedSDIds)
    speedChunk = read(speedSDIds);
    speedChunkData = speedChunk.Data;
    latestMax = max([speedChunkData; latestMax]);
end

latestMax
latestMax = 300

On each read operation, the read function updates the read position for the start of the next read operation. After reading some or all of the sdidatastore object, you can reset the read position to start again from the beginning of the signal.

reset(speedSDIds)

Process Signal Data in Memory

When the signal referenced by your sdidatastore object fits into memory, you can use the readall function to read all the signal data into memory for processing rather than reading and processing the data incrementally with the read function. The readall function returns a timetable with all the signal data.

speedTimetable = readall(speedSDIds);
speedMax = max(speedTimetable.Data)
speedMax = 300

You can log big data from a simulation and inspect and analyze portions of that data by interacting with a matlab.io.datastore.SimulationDatastore object.

Log Big Data from Model

Open the model sldemo_fuelsys.

mdl = "sldemo_fuelsys";
open_system(mdl)

Select Configuration Parameters > Data Import/Export > Log Dataset data to file to log data to persistent storage. Alternatively, you can log data using Dataset format to a MAT-file instead of the workspace programmatically.

set_param(mdl,"LoggingToFile","on")

Simulate the model.

sim(mdl);

The MAT-file out.mat appears in your current folder. Logged signal data is stored in the MAT-file with the variable name sldemo_fuelsys_out.

Create a DatasetRef object that refers to the logged signal data.

DSRef = Simulink.SimulationData.DatasetRef("out.mat","sldemo_fuelsys_output");

Preview Big Data

Use curly braces to return a SimulationDatastore representation of the fuel signal, which is the tenth element in the DatasetRef object DSRef. The SimulationDatastore object exists in the Values property of the returned Signal object.

SimDataSig = DSRef{10};
DStore = SimDataSig.Values;

Use the preview function to inspect the first ten samples of logged data for the fuel signal.

preview(DStore)
ans=10×1 timetable
         Time          Data 
    ______________    ______

    0 sec              1.209
    0.00056199 sec     1.209
    0.0033719 sec      1.209
    0.01 sec          1.1729
    0.02 sec          1.1409
    0.03 sec          1.1124
    0.04 sec          1.0873
    0.05 sec          1.0652
    0.055328 sec      1.0652
    0.055328 sec      1.0652

Inspect Specific Sample

Suppose you want to inspect the 603rd sample of logged fuel data. Set the ReadSize property of DStore to a number that, considering memory resources, your computer can tolerate. For example, set ReadSize to 200.

DStore.ReadSize = 200;

Read from the datastore three times. Each read operation advances the reading position by 200 samples.

read(DStore);
read(DStore);
read(DStore);

Now that you are close to the 603rd sample, you can set ReadSize to a smaller number to make the target sample easier to find. For example, set ReadSize to 5.

DStore.ReadSize = 5;

Read from the datastore again. The third sample of read data is the 603rd sample in the datastore.

read(DStore)
ans=5×1 timetable
      Time       Data 
    ________    ______

    5.79 sec    1.6097
    5.8 sec     1.6136
    5.81 sec    1.6003
    5.82 sec    1.5904
    5.83 sec    1.5832

Inspect Earlier Sample

You can also inspect an earlier sample. For example, inspect the 403rd sample of logged fuel data. Due to previous read operations, the datastore now reads starting from the 606th sample.

Use the reset function to reset DStore. Then, read from the first sample up to the 403rd sample.

reset(DStore);

Set ReadSize to 200.

DStore.ReadSize = 200;

Read from the datastore twice to advance the read position to the 401st sample.

read(DStore);
read(DStore);

Set ReadSize to 5 and read from the datastore. Now, the third sample of read data is the 403rd sample in the datastore.

DStore.ReadSize = 5;
read(DStore)
ans=5×1 timetable
      Time       Data  
    ________    _______

    3.85 sec      0.999
    3.86 sec    0.99219
    3.87 sec    0.98538
    3.88 sec    0.97858
    3.89 sec    0.97179

Extract Multiple Samples

You can also use the read function to extract multiple samples. For example, extract samples 1001 through 1020.

Reset the datastore. Then, advance to sample 1001 by setting the ReadSize property to 200 and reading the datastore five times.

reset(DStore)

DStore.ReadSize = 200;
for i = 1:5
    read(DStore);
end

Set the ReadSize to 20 to extract 20 samples from the datastore.

DStore.ReadSize = 20;

Extract samples 1001 through 1020. Store the extracted data in a variable named targetSamples.

targetSamples = read(DStore)
targetSamples=20×1 timetable
      Time       Data 
    ________    ______

    9.7 sec     1.5828
    9.71 sec    1.5733
    9.72 sec    1.5664
    9.73 sec    1.5614
    9.74 sec    1.5579
    9.75 sec    1.5553
    9.76 sec    1.5703
    9.77 sec     1.582
    9.78 sec    1.5913
    9.79 sec    1.5988
    9.8 sec      1.605
    9.81 sec    1.6101
    9.82 sec    1.6145
    9.83 sec    1.6184
    9.84 sec    1.6049
    9.85 sec     1.595
      ⋮

Find Maximum Value of Data in Datastore

Use the hasdata function as the condition for a while loop to incrementally analyze the data in chunks of 200 samples.

reset(DStore);
DStore.ReadSize = 200;
runningMax = [];
while hasdata(DStore)
    tt = read(DStore);
    rawChunk = tt.Data;
    runningMax = max([rawChunk; runningMax]);
end

The variable runningMax stores the maximum value in the entire datastore.

runningMax
runningMax = 1.6423

Input Arguments

collapse all

Datastore to read, specified as a matlab.io.datastore.sdidatastore or matlab.io.datastore.SimulationDatastore object.

Datastore to read, specified as a matlab.io.datastore.SimulationDatastore object.

Output Arguments

collapse all

Chunk of data read from datastore, returned as a timetable object. For information about timetable objects, see Timetables.

Information about read data, returned as a structure array. The structure has one field, FileName, which is a fully resolved path containing the path string, the name of the file, and the file extension.

Alternatives

When working with matlab.io.datastore.sdidatastore datastores, you can use your sdidatastore object to create a tall timetable to process signals too large to fit into memory. The tall timetable handles loading and processing the chunks of signal data for you. For more information about working with tall timetables, see Tall Arrays.

Version History

Introduced in R2017b