reading huge data files with repeating pattern

5 visualizzazioni (ultimi 30 giorni)
Hi!
I want to read a file as I have attached in the example. Each timestep is independent (there are about 2M timesteps in each file) and I know the number of lines in each file. Can someone please help me with this?

Risposta accettata

Star Strider
Star Strider il 30 Mag 2022
This is not an easy file to work with.
One approach —
fidi = fopen('example.txt','rt');
k1 = 1;
while ~feof(fidi)
for k = 1:9
first9{k1,k} = fgetl(fidi) % First 9 Lines
end
C = textscan(fidi, ['%f%f%s' repmat('%f',1,9)], 'CollectOutput',true);
C3{k1,:} = [C{:,2}] % Column #3
M = cell2mat(C(:,[1 3]));
if isempty(M) % Empty Matrix Indicates End-Of-File
break
end
D{k1,:} = M;
fseek(fidi, 0, 0);
k1 = k1 + 1
end
fclose(fidi);
C3v = cat(1,C3{:}) % Column #3 (Column Vector)
Out = cell2mat(D); % Numeric Data Matrix
This reads the entire file, saving the data as a (48x11) double matrix. The third column of the data are saved as ‘C3v’ and here is a (48x1) cell array of strings corresponding to the rows of ‘Out’ The first 9 lines of each section are saved in the ‘first9’ cell array for you to do with them as you wish later. When the code encounters an empty matrix ‘M’ it exits the loop. This prevents an infinite loop if a valid end-of-file indicator is not found.
.
  2 Commenti
GUNJAN AUTI
GUNJAN AUTI il 30 Mag 2022
Thanks a lot! I was trying to loop for each value and created an infinite loop. This will be much faster than my method!
Star Strider
Star Strider il 30 Mag 2022
As always, my pleasure!
This is the approach I usually use with such files.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by