Proper reading of .txt
조회 수: 1 (최근 30일)
이전 댓글 표시
I would apreciate your contribution on the following:
I have several txt files (see attached 2 of them) correspong to weather stations (see Stations coordinates.txt), which I want to access and then do a number of things, eg. find mean values. The thing is, I cannot read them in the proper way using textscan. Of course the format I used ('%d %d %d') is all wrong but I can't find the correct one.
list1 = rdir ('C:\Projects\LIFE ASTI\C.3\C.3\Weather station data\from desktop\OutputObservations\');
obsdir = 'C:\Projects\LIFE ASTI\C.3\C.3\Weather station data\from desktop\OutputObservations\*.txt';
stations = readtable('C:\Projects\LIFE ASTI\C.3\C.3\Weather station data\from desktop\Stations coordinates.txt');
output_path='C:\Projects\LIFE ASTI\C.3\C.3\Weather station data\from desktop\OutputObservations';
% Variables
vars={'Temperature'; 'Relative humidity'};
years = {'2015'; '2019'};
varunits={'Celsius'; '%' };
%Starting loop for all files
for i=1:size(stations,1)
name = stations( i, 1 );
network = stations (i, 'Network');
lat = stations(i, 'Lat' );
lon = stations(i, 'Lon' );
%% Here we make the selection based on network and station
network.(1);
name.(1);
networkstr = char(network.(1));
namestr = char(name.(1));
indx = strfind (list1, namestr) ;
for j = 1:size(indx, 1)
if (indx{j,1}>0)
if (strfind (list1{j,1}, '2015') > 0)
if (strfind(list1{j,1}, 'txt') > 0)
disp(list1{j,1})
fileID = fopen(list1{j,1});
B = textscan(fileID,'%d %d %d');
fclose(fileID);
end
end
end
end
end
댓글 수: 0
채택된 답변
J. Alex Lee
2020년 1월 24일
Just use readtable()? Looks like every station will have already-aggregated results (hourly vs daily for the same station, same year). You should be able to re-generated the daily data from the hourly data. If hourly is as fine-grained as you have, I would suggest using only those and working with timetables. readtable is smart enough and your dates are formatted well enough to be automatically imported as datetimes
댓글 수: 2
J. Alex Lee
2020년 1월 24일
Glad it works for you, but I'm confused about why it would work for a file like your hourly example, which does not contain only numeric data. If you restrict your files to hourly files, you could do
T = readtimetable(filename)
And then you can recover the daily averages with something like
daily = retime(T,'daily',@(x)mean(x,'omitnan'))
or insert "monthly", or "weekly", etc.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!