Using while loop to analyze lines repeated

2 views (last 30 days)
pink flower
pink flower on 5 Oct 2020
Commented: pink flower on 5 Oct 2020
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 Comments
Geoff Hayes
Geoff Hayes on 5 Oct 2020
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 on 5 Oct 2020
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

Sign in to comment.

Answers (0)

Categories

Find more on Environment and Settings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!