MATLAB Examples

Write and Read Character Data from Binary Files in Simulink

The Binary File Writer and Binary File Reader blocks 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.

Contents

Write the Character Data

Cast the character data, 'binary_file' into uint8 using the cast function.

data = 'binary_file';

Write the cast data to the data file myCharFile.dat.

writeModel = fullfile(matlabroot,'examples','dsp','writeCharData');
open_system(writeModel)
sim(writeModel)

Read the uint8 Data

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

readModel = fullfile(matlabroot,'examples','dsp','readCharData');
open_system(readModel)
sim(readModel);
charData = char(readerData);

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

strcmp(data,charData.')
ans =

  logical

   1