Main Content

Write and Read Matrix Data

Use a dsp.BinaryFileReader System object™ to read data from a binary file in a row-major format.

Write the Data

Write the matrix A to the binary file Matdata.bin using a dsp.BinaryFileWriter object. The object writes the specified header followed by the data.

The header has the following format:

  • DataType set to double.

  • Complexity set to false.

  • FrameSize (number of rows in the data matrix) set to 3.

  • NumChannels (number of columns in the data matrix) set to 4.

A = [1 2 3 8; 4 5 6 10; 7 8 9 11];
header = struct('DataType','double',...
writer = dsp.BinaryFileWriter('Matdata.bin',...

Release the writer so that the reader can access the data.


Read the Data

Specify the header using the HeaderStructure property of the reader object. If the exact header is not known, you must at least specify the prototype of the header. That is, the number of fields, and the data type, size, and complexity of each field in the prototype must match with the header data written to the binary file. The dsp.BinaryFileReader object reads the binary file Matdata.bin until the end of file is reached. Configure the System object to read the data into 4 channels, with each channel containing 5 samples. Each loop of the iteration reads a channel (or frame) of data.

headerPrototype = struct('DataType','double',...
reader = dsp.BinaryFileReader('Matdata.bin',...
while ~isDone(reader)
    out = reader();
out = 5×4

     1     2     3     8
     4     5     6    10
     7     8     9    11
     0     0     0     0
     0     0     0     0

Each frame of out contains frames of the matrix A, followed by zeros to complete the frame. The original matrix A contains 4 channels with 3 samples in each channel. The reader is configured to read data into 4 channels, with each channel containing 5 samples. Because there are not enough samples to complete the frame, the reader object appends zeros at the end of each frame.

See Also


Related Topics