How to find a value in a file from the index of another array?

조회 수: 4 (최근 30일)
pink flower
pink flower 2020년 10월 1일
편집: pink flower 2020년 10월 1일
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
Walter Roberson
Walter Roberson 2020년 10월 1일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2020년 10월 1일
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
pink flower
pink flower 2020년 10월 1일
편집: pink flower 2020년 10월 1일
I got this error.
"Error using load
Number of columns on line 2 of ASCII file /home/amanda/Documentos/CODIGOS_RADAR/DADOS_ZDR/2014/jan2014/ZDR_02000_20140101_0000.dat must be the same as previous lines."
The .dat file is a 500 x 500 matrix. How can I use fopen to open it?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Low-Level File I/O에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by