How can I loop fread through the same data file multiple times ?

4 Ansichten (letzte 30 Tage)
LO
LO am 8 Okt. 2018
Kommentiert: dpb am 8 Okt. 2018
I have a large binary related to 1 hour recording from 16 electrodes (each of them part of a "pair"). The binary data has in the file the following structure: 1 = time 2 = channel 1 3 = channel 2 ... 17 = channel 16
I am trying to loop fread in order to load a segment of 1 minute for each channel, perform some analysis, save a mat file and repeat with the following channel. The script works for a single channel using a while loop. I am trying to put the while loop nested in a for loop by setting the parameter "activepair" as variable from 1 to "total number of pairs in my data"
activepair=1; %this value is set at the beginning to select the pair. Here is where I want to put a "for" cycle to go from 1 to N pairs, depending on what's in the long binary file.
while ~feof(FID)
tic
index=index+1; % second number
currData=fread(FID,[Fs*60*(n_channels+1),1],'double'); % read one minute of data in 2D array
disp(sprintf('Processing minute: %d',index))
disp(sprintf('Processing pair: %d',activepair))
%%loading channel 1
time = currData((1:(n_channels+1):end), 1);
data1= currData(((activepair+1):(n_channels+1):end), 1);
trace1 = [time data1];
seg1 = transpose(trace1(:, 2));
%%load channel 2
data2 = currData(((activepair+2):(n_channels+1):end), 1);
trace2 = [time data2];
seg2 = transpose(trace2(:,2));
...CALCULATIONS HERE...
toc
if index > 59, break, end ; %this line prevents the cycle to attempt analyzing data starting at the end of the last segment, as a while cycle would normally try to do. In case of recordings shorter or longer than 60 minutes, modify the value as "end-1" minutes (z.b. 29 for a 30 min recording)
end
However for some reason the script is not able to repeat. the variable currData results empty at the end of the first while loop and I receive an error related to the inconsistency of the matrix sizes used... I think somehow the reference to the beginning of the file... where fread should start, gets lost somewhere but I am not sure this is the issue.
  1 Kommentar
dpb
dpb am 8 Okt. 2018
Well, by the end of the while loop you're at EOF so any further fread after that is bound to return empty.
I don't see any frewind or fseek to set the file pointer back to the beginning or the location of a timestep data segment anywhere.
You might want to look at memmapfile as way to build structure of the internal storage that can then address at higher level.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Low-Level File I/O finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by