Info

この質問は閉じられています。 編集または回答するには再度開いてください。

How to use if/else to skip repeated lines?

3 ビュー (過去 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