Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

How to use if/else to skip repeated lines?

조회 수: 1 (최근 30일)
pink flower
pink flower 2020년 10월 3일
마감: MATLAB Answer Bot 2021년 8월 20일
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!

답변 (1개)

Walter Roberson
Walter Roberson 2020년 10월 3일
편집: Walter Roberson 2020년 10월 3일
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
pink flower
pink flower 2020년 10월 3일
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!

Translated by