Main Content

Write and Read Character Data

The dsp.BinaryFileWriter and dsp.BinaryFileReader System objects do not support writing and reading characters. As a workaround, cast character data to one of the built-in data types and write the integer data. After the reader reads the data, convert the data to a character using the char function.

Write the Character Data

Cast a character into uint8 using the cast function. Write the cast data to the data file myFile.dat.

data = 'binary_file';
castData = cast(data,'uint8');
writer = dsp.BinaryFileWriter('myFile.dat');

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


Read the uint8 Data

Configure the reader to read the cast data as uint8 data.

reader = dsp.BinaryFileReader('myFile.dat',...
readerData = reader();
charData = char(readerData);

Verify that the writer data is the same as the reader data. By default, the reader returns the data in a column-major format.

ans = logical

See Also


Related Topics