How to find a value in a file from the index of another array?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have a data column with 7 columns. Based on column 1, 2 and 7 of the .txt file, I need to open my .dat file. Next, I need to find the value of the variable in the .dat file for the index (column 3 of the .txt file). How can I do this? This is a part of my .txt file:
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 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 139904 -7.259 -34.982 23.0 03000
20140101 0000 140375 -6.998 -34.973 22.2 02000
20140101 0000 140404 -7.259 -34.973 24.5 03000
20140101 0000 140903 -7.250 -34.964 22.2 03000
5 commentaires
Walter Roberson
le 1 Oct 2020
You should not use %0 format specifications for input.
Using a format width is not needed in this case, as the columns are well separated.
Réponses (1)
Walter Roberson
le 1 Oct 2020
datfiledir = 'appropriate directory';
txtdata = load('YourFile.txt'); %don't worry, it is all numeric
nrow = size(txtdata,1);
outputs = [];
for K = 1 : nrow
key = txtdata(K,3);
filename = fullfile(datfiledir, sprintf('%08d_%04d_%05d.dat'. txtdata(K, [1 2 7])));
datdata = load(filename, '-ascii');
[found, idx] = ismember(key, datdata(:,1)); %the 1 needs to be adjusted according to the dat format
if found
outputs(K,:) = datdata(idx,:);
else
outputs(K,:) = nan(1,size(datdata,2));
fprintf('Warning: key %d not found for row %d in file "%s"\n', key, K, filename);
end
end
1 commentaire
Voir également
Catégories
En savoir plus sur Standard File Formats dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!