hasdata
R2026bDetermine whether data is available to read
Syntax
Description
Examples
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. This example is meant as a demonstration and does not log big data.
Log Big Data from Model
Open the model ThreeSigs.
mdl = "ThreeSigs";
open_system(mdl)To log data to persistent storage, select Configuration Parameters > Data Import/Export > Log data to file. Alternatively, you can log data in Dataset format to a MAT file instead of the workspace programmatically.
set_param(mdl,"LoggingToFile","on")
Set the stop time of the simulation to 3000 seconds and simulate the model.
set_param(mdl,StopTime="3000")
out = sim(mdl);The MAT file out.mat appears in your current folder. The software stores logged output data in the MAT file with the variable name yout.
Create a DatasetRef object that refers to the logged signal data.
DSRef = Simulink.SimulationData.DatasetRef("out.mat","yout");
Preview Big Data
To return a SimulationDatastore representation of the sineSig signal, which is the first element in the DatasetRef object DSRef, use curly braces. The SimulationDatastore object exists in the Values property of the returned Signal object.
SimDataSig = DSRef{1};
DStore = SimDataSig.Values;To inspect the first ten samples of logged sineSig data, use preview.
preview(DStore)
ans = 10×1 timetable
Time Data
_______ ________
0 sec 0
0.1 sec 0.099833
0.2 sec 0.19867
0.3 sec 0.29552
0.4 sec 0.38942
0.5 sec 0.47943
0.6 sec 0.56464
0.7 sec 0.64422
0.8 sec 0.71736
0.9 sec 0.78333
Inspect Specific Sample
Suppose you want to inspect the 603rd sample of logged sineSig 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
________ ________
60 sec -0.30481
60.1 sec -0.39837
60.2 sec -0.48795
60.3 sec -0.57265
60.4 sec -0.65164
Inspect Earlier Sample
To inspect an earlier sample, reset the datastore to start reading from the first sample. For example, inspect the 403rd sample of logged sinSig data. Due to previous read operations, the datastore now reads starting from the 606th sample.
To read DStore from the beginning, use reset. Then, set ReadSize to 200 and read from the datastore twice to advance the read position to the 401st sample.
reset(DStore); DStore.ReadSize = 200; 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
________ _______
40 sec 0.74511
40.1 sec 0.67481
40.2 sec 0.59776
40.3 sec 0.51474
40.4 sec 0.42658
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
_________ ________
100 sec -0.50637
100.1 sec -0.41775
100.2 sec -0.32496
100.3 sec -0.22892
100.4 sec -0.13059
100.5 sec -0.03096
100.6 sec 0.06898
100.7 sec 0.16823
100.8 sec 0.2658
100.9 sec 0.36072
101 sec 0.45203
101.1 sec 0.53882
101.2 sec 0.62023
101.3 sec 0.69544
101.4 sec 0.76371
101.5 sec 0.82434
⋮
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.0000
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 ThreeSigs model, which is configured to log three signals, to create data in the Simulation Data Inspector repository.
mdl = "ThreeSigs";
out = sim(mdl);The model is configured to log data using a single Simulink.SimulationOutput object named out. The SimulationOutput object contains the logged output data in a Simulink.SimulationData.Dataset object named yout. Use dot notation to access the logged data.
out.yout
ans =
Simulink.SimulationData.Dataset 'yout' with 3 elements
Name BlockPath
________ ______________
1 [1x1 Signal] sineSig ThreeSigs/Out1
2 [1x1 Signal] randSig ThreeSigs/Out2
3 [1x1 Signal] chirpSig ThreeSigs/Out3
- Use braces { } to access, modify, or add elements using index.
Get the signal ID for the signal named sineSig.
runCount = Simulink.sdi.getRunCount;
latestRunID = Simulink.sdi.getRunIDByIndex(runCount);
latestRun = Simulink.sdi.getRun(latestRunID);
sineSigID = getSignalIDsByName(latestRun,"sineSig");Use the signal ID to create an sdidatastore object for the sineSig signal.
sineSDIds = matlab.io.datastore.sdidatastore(sineSigID);
Verify Contents of Datastore
Check the Name property of the sdidatastore object to verify that it matches your expectations.
sineSDIds.Name
ans = 'sineSig'
You can also use the preview function to verify the first ten samples in the signal.
preview(sineSDIds)
ans = 10×1 timetable
Time Data
_______ ________
0 sec 0
0.1 sec 0.099833
0.2 sec 0.19867
0.3 sec 0.29552
0.4 sec 0.38942
0.5 sec 0.47943
0.6 sec 0.56464
0.7 sec 0.64422
0.8 sec 0.71736
0.9 sec 0.78333
Process Signal Data with sdidatastore Object
When your signal is too large to fit into memory, use read to load and incrementally process data from the Simulation Data Inspector repository in chunks. Use hasdata as the condition for a while loop to incrementally process the whole signal. For example, find the maximum signal value.
latestMax = []; while hasdata(sineSDIds) sineChunk = read(sineSDIds); sineChunkData = sineChunk.Data; latestMax = max([sineChunkData; latestMax]); end latestMax
latestMax = 0.9996
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(sineSDIds)
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. The readall function returns a timetable with all the signal data.
sineTimetable = readall(sineSDIds); sineMax = max(sineTimetable.Data)
sineMax = 0.9996
Input Arguments
Datastore to check for available data to read, specified as a
matlab.io.datastore.sdidatastore,
matlab.io.datastore.simulationdatastore object, or
matlab.io.datastore.MLDATXSignalDatastore
object.
Output Arguments
Data availability indication, returned as a 1 or
0 of data type logical. When the
sdidatastore object has data available to read,
tf is 1. When data is not
available, tf is 0.
Version History
Introduced in R2017b
See Also
Objects
matlab.io.datastore.sdidatastore|matlab.io.datastore.SimulationDatastore|Simulink.sdi.DatasetRef
Functions
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Sélectionner un site web
Choisissez un site web pour accéder au contenu traduit dans votre langue (lorsqu'il est disponible) et voir les événements et les offres locales. D’après votre position, nous vous recommandons de sélectionner la région suivante : .
Vous pouvez également sélectionner un site web dans la liste suivante :
Comment optimiser les performances du site
Pour optimiser les performances du site, sélectionnez la région Chine (en chinois ou en anglais). Les sites de MathWorks pour les autres pays ne sont pas optimisés pour les visites provenant de votre région.
Amériques
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)