reading huge data files with repeating pattern

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?

 채택된 답변

Star Strider
Star Strider 2022년 5월 30일

0 개 추천

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

Thanks a lot! I was trying to loop for each value and created an infinite loop. This will be much faster than my method!
As always, my pleasure!
This is the approach I usually use with such files.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

질문:

2022년 5월 30일

댓글:

2022년 5월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by