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

    %% Write and Read Binary Files
% Create a binary file with a custom header using the |dsp.BinaryFileWriter|
% System object(TM). Write data to this file. Read the header and data
% using the |dsp.BinaryFileReader| System object.

%% Write the Data
% Specify the file header and create a |dsp.BinaryFileWriter| object.
% The object writes the header first, followed by the data,  to the 
% |ex_file.bin| file. The data is a noisy sine wave signal. View the data 
% in a time scope.
header = struct('A',[1 2 3 4],'B','x7');
writer = dsp.BinaryFileWriter('ex_file.bin','HeaderStructure',header);
L = 150;
sine = dsp.SineWave('SamplesPerFrame',L);
scopewriter = dsp.TimeScope(1,'YLimits',[-1.5 1.5],'SampleRate',...
    L,'TimeSpan',1);

for i = 1:1000
    data = sine()+0.01*randn(L,1);
    writer(data);
    scopewriter(data);
end

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

%% 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, its size and data type. The 
% |dsp.BinaryFileReader| object reads the binary data from |ex_file.bin|
% until the end of file is reached.
% The data is read into a single channel (column) containing multiple
% frames, where each frame has 300 samples. View the data in a time scope.
headerPrototype = struct('A',[0 0 0 0],'B','-0');

reader = dsp.BinaryFileReader(...
    'ex_file.bin',...
    'HeaderStructure',headerPrototype,...
    'NumChannels',1,'SamplesPerFrame',300);
scopereader = dsp.TimeScope(1,'YLimits',[-1.5 1.5],'SampleRate',...
    L,'TimeSpan',1);
while ~isDone(reader)
    out = reader();
    scopereader(out);
end
release(reader);

%%
% Even when the reader reads data with a different frame size, the output 
% in both time scopes matches exactly.