www.gusucode.com > dsp 案例源码程序 matlab代码 > dsp/WriteAndReadCharacterDataExample.m

    %% 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');
writer(castData);

%% 
% Release the writer so that the reader can access the data.
release(writer);

%% Read the |uint8| Data
% Specify the reader to read the cast data as |uint8| data. 
reader = dsp.BinaryFileReader('myFile.dat','DataType','uint8','SamplesPerFrame',11);
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. 
strcmp(data,charData.')