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 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!