Info

This question is closed. Reopen it to edit or answer.

How to use if/else to skip repeated lines?

1 view (last 30 days)
pink flower
pink flower on 3 Oct 2020
Closed: MATLAB Answer Bot on 20 Aug 2021
I have a file like this:
20140101 0000 69760 -5.965 -36.250 26.0 02000
20140101 0000 69761 -5.974 -36.250 23.5 02000
20140101 0000 73180 -5.247 -36.187 23.5 02000
20140101 0000 73678 -5.229 -36.178 26.5 03000
20140101 0000 74178 -5.229 -36.169 26.5 03000
20140101 0000 128828 -6.576 -35.181 22.6 04000
From columns 1, 2 and 7, I extract the information to open the .dat files.
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:2;
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
How to use if/else function to open each file only once?!
Thanks!

Answers (1)

Walter Roberson
Walter Roberson on 3 Oct 2020
Edited: Walter Roberson on 3 Oct 2020
unique(data(:,[1 2 7]) with 'stable', and take the second output of unique, which will be a vector of row indices of the first line that has any particular unique value combination.
  1 Comment
pink flower
pink flower on 3 Oct 2020
I didn't understand it very well ... how can I explain it better?

Community Treasure Hunt

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

Start Hunting!