Using while loop to analyze lines repeated

조회 수: 2 (최근 30일)
pink flower
pink flower 2020년 10월 5일
댓글: pink flower 2020년 10월 5일
Hello, everybody. I have a doubt:
How can I compare three of the 7 columns in a file? I have a data file with 7 columns.
data = load('/home/pink/input.txt');
filenames = compose('precipi_%05d_%08d_%04d.dat', data(:,7), data(:,1), data(:,2));
nfiles = numel(filenames);
for K = 1:nfiles;
thisfile = filenames(K);
fileID = fopen(thisfile{1});
dados = fread(fileID,[500 500], 'float32');
dados((dados==-99.0))=NaN;
fclose(fileID);
end
However, I need to open each file only once, that is, the file precipi_02000_20140101_0000.dat, for example, can only be opened once. So I thought of doing an if / else for this. If the second line is the same as the first, considering column 1, 2 and 7, skip to the third line. If the third line is different from the previous one, the file will be opened. Then in this case the files will be opened twice considering the part of the file exposed above:
precipi_02000_20140101_0000.dat
precipi_03000_20140101_0000.dat
precipi_04000_20140101_0000.dat
I'm trying to do:
while ymd (K) == ymd (K==K+1);
K = K+1;
end
K = K-1;
where ymd is the first column (20140101), for example.
How to use while function to open each file only once?!
Thanks!
  댓글 수: 2
Geoff Hayes
Geoff Hayes 2020년 10월 5일
pink - you say If the third line is different from the previous one, the file will be opened. What do you mean bu this? Which file will be opened? Please clarify what you are comparing within each file.
pink flower
pink flower 2020년 10월 5일
The file has numerous lines. Assuming that the first file is opened based on columns 1, 2 and 7, if the values ​​in columns 1, 2 and 7 on the next line are the same as the previous one, then it skips to the next line.
This is an excerpt from the file called data:
20140101 0000 73678 -5.229 -36.178 26.5 02000
20140101 0000 74178 -5.229 -36.169 26.5 02000
20140101 0000 128828 -6.576 -35.181 22.6 03000
20140101 0000 138373 -6.980 -35.009 20.8 02000
20140101 0000 139404 -7.259 -34.991 22.0 03000
20140101 0000 153914 -7.348 -34.728 29.0 04000
Then, the files will be opened:
precipi_02000_20140101_0000.dat
precipi_03000_20140101_0000.dat
precipi_04000_20140101_0000.dat

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by