read
Description
reads all data from the data = read(streamReader)adtfStreamReader
object streamReader across all streams, and returns it in the form of
the structure array, data.
Examples
This example shows how to extract and visualize a video stream, stored in an ADTF DAT file. It also shows how to write the video stream into a video file.
Download the sample video DAT file.
downloadURL = 'https://ssd.mathworks.com/supportfiles/driving/data/sample_can_video_dat.zip'; dataFolder = fullfile(tempdir, 'adtf-video', filesep); options = weboptions('Timeout', Inf); zipFileName = [dataFolder, 'sample_can_video_dat.zip']; folderExists = exist(dataFolder, 'dir'); % Create a folder in a temporary directory to save the downloaded file. if ~folderExists mkdir(dataFolder); disp('Downloading sample_can_video_dat.zip (9.74 MB)...') websave(zipFileName, downloadURL, options); % Extract contents of the downloaded file. disp('Extracting sample_can_video_dat.zip...') unzip(zipFileName, dataFolder); end
Downloading sample_can_video_dat.zip (9.74 MB)...
Extracting sample_can_video_dat.zip...
Create the ADTF File Reader object.
datFileName = fullfile(dataFolder,"sample_can_video.dat");
fileReader = adtfFileReader(datFileName) fileReader =
DataFileName: "C:\Users\latriwal\AppData\Local\Temp\adtf-video\sample_can_video.dat"
DescriptionFileName: ""
PluginDirectory: ""
StreamCount: 2
StreamInfo:
StreamIndex StreamName StreamType StartTime EndTime ItemCount SubstreamInfo
___________ __________ ______________ _________ __________ _________ _____________
1 {'rawcan'} {'UNRESOLVED'} 0 sec 14.805 sec 743 {0×1 struct}
2 {'video' } {'adtf/image'} 0 sec 14.799 sec 149 {0×1 struct}
From the StreamInfo property, note that the index of the video stream is 2. Use the select function of the adtfFileReader object, to select the video stream for reading. The returned adtfStreamReader object has all the information about the selection.
streamReader = select(fileReader,2)
streamReader =
adtfStreamReader with properties:
DataFileName: "C:\Users\latriwal\AppData\Local\Temp\adtf-video\sample_can_video.dat"
DescriptionFileName: ""
PluginDirectory: ""
StreamIndex: 2
StartIndex: 1
EndIndex: 149
CurrentIndexOffset: 0
StartTime: [0×0 duration]
EndTime: [0×0 duration]
DataCount: 149
Note that the value of CurrentIndexOffset is 0. This signifies that the next readNext call will return the first item.
Preview the first image frame from the stream.
firstFrame = readNext(streamReader); imshow(firstFrame.Data.Item)

Before creating a video, use the reset function to start reading from the first frame. This resets the value of CurrentIndexOffset to 0.
reset(streamReader);
fprintf("CurrentIndexOffset = %d\n",streamReader.CurrentIndexOffset)CurrentIndexOffset = 0
Create a VideoWriter object that you can use to write image frames to a video file. Specify a frame rate of 1 frame per second.
videoWriterObj = VideoWriter("example_video.avi");
videoWriterObj.FrameRate = 1;
open(videoWriterObj); Using the streamReader object, iterate over the data items in the selection one-by-one. The hasNext function determines if there is an item left to read as we are incrementally reading the file. readNext returns the data item which is basically a structure containing the data and the associated timestamp. In every iteration, extract the image frame and write it to the video file.
while streamReader.hasNext() streamData = streamReader.readNext(); imageFrame = streamData.Data.Item; frame = im2frame(streamData.Data.Item, gray); writeVideo(videoWriterObj, frame); end
Alternatively, you can read all the image frames at once, using the read function, and iterate over it later.
allData = read(streamReader)
allData = struct with fields:
StreamIndex: 2
Data: [149×1 struct]
Close the connection with the video file.
close(videoWriterObj);
close allVisualize the output file example_video.avi using Video Viewer.
implay("example_video.avi") ![{"String":"Figure Movie Player contains an axes object and other objects of type uiflowcontainer, uimenu, uitoolbar. The axes object contains an object of type image.","Tex":[],"LaTex":[]}](../../examples/driving/win64/ExtractVideoStreamFromADTFDATFileExample_02.png)
Input Arguments
Stream reader, specified as an adtfStreamReader object.
Output Arguments
All data items from the selection across all streams, returned as an
N-by-1 structure array. N is
the number of streams in the selection. Each element in data is a
structure that has these fields:
StreamIndex— Index of the stream the data comes from.Data— Structure array containing all data items from the stream. Each structure in the array has these fields:ChunkTimestamp— Time at which the data item was written to the DAT file.SampleTimestamp— Time at which the data item was collected at the source.Item— Data item.ItemName— Name of the structure stored inItem.IsValid— Flag indicating whether the read operation was successful, stored as alogicalvalue.SubstreamIndex— Index of the substream, if the data item belongs to one.
Version History
Introduced in R2022a
See Also
adtfFileReader | select | adtfStreamReader | readNext | hasNext | reset
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.
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)