Is there a more efficient way to read a .txt data file into MATLAB?

1 view (last 30 days)
I have this .txt data file
Date Tmin°C Tmax°C rain cm snow cm
1950-01-01 -22.100 -3.300 0.000000 0.020000
1950-01-02 -18.800 -1.600 0.000000 0.050000
...
I want to read it and save it in a var (structure or cell)to use the information in my programme. I need to keep date for ref and header to make this file more easy to other user.
How to do that efficiently?
EDIT: Follow up (should have been a separate question...)
I always need to find monthly mean of each years
I use the same .txt data file of the previous question results shoud be like this ex:
Jan 1950 = -20
Jan 1951 = -22
Feb 1950 = -24
Feb 1951 = -23
In a structure or cell or one var for each mounth
The code I did to do that is very primitive, and the 29 feb is complicated to deal so my code is slow, heavy.

Accepted Answer

Matt Tearle
Matt Tearle on 18 Feb 2011
If the file is tab-delimited, this will do it:
fid = fopen('filename.txt');
headers = textscan(fid,'%s%s%s%s%s',1,'delimiter','\t')
data = textscan(fid,'%s%f%f%f%f','delimiter','\t')
fclose(fid);
If it's space-delimited, that first line will cause you trouble, because of the spaces in the headers. You'll have to work out how to parse that. You can read the line with fgetl, then do some regular expression parsing to work out where the breaks go (more than one space, I guess).
EDIT: Or perhaps I've misunderstood you. When you say you need to keep the date and header, do you mean you need to read them into MATLAB or you need to keep them in the file (but not read them)? In the latter case, remove the headers = ... line and replace the next line with:
data = textscan(fid,'%*s%f%f%f%f','delimiter','\t','headerlines',1)
  3 Comments
Jonathan Roy
Jonathan Roy on 18 Feb 2011
Great! Thank you guys for so fast help!!
May be a last question... I always need to find mounthly mean of each years
i use the same .txt data file of the previous question results shoud be like this ex: Jan 1950 = -20 Jan 1951 = -22 ... feb 1950 = -24 feb 1951 = -23
in a structure or cell or one var for each mounth
the code I did to do that is very primitive, and the 29 feb is complicated to deal so my code is slow, heavy and I wish someone have creat somethinh better but i find nothing....may be you know one to suggest me!??

Sign in to comment.

More Answers (4)

R. Scott
R. Scott on 18 Feb 2011
If you have the stats toolbox then look at using a 'dataset' you could even import your .txt file directly into a dataset.

R. Scott
R. Scott on 18 Feb 2011
Have you looked at the fun 'textscan'
fid = fopen('fileToRead')
data = textscan(fid, ' delimiters here %s%d...', 'delimiter', ',', 'headerlines',0)
fclose(fid)
play around with textscan or enter textscantool in the command window.

Matt Tearle
Matt Tearle on 18 Feb 2011
BTW, Jonathan, a good practice is to ask a separate new question, because this second question might be something someone else might be looking for.
Anyway, have a look at the various date conversion functions datenum, datestr, and datevec. At the moment you have a cell array of date strings. To manipulate dates in terms of days/months/years, it's probably easiest to convert to date vectors using datevec. This will give you a matrix, one row for each date; the columns being the years, months, and days. Something like this, then, would enable you to do monthly analysis:
dates = data{1}; % dates is a cell array of date strings
mintemp = data{2}; % mintemp is double array
dtvc = datevec(dates);
yrs = dtvc(:,1);
mths = dtvc(:,2);
for k = 1:12
idx = (yrs==1950) & (mths==k); % logical index
mintemps_thismonth = mintemp(idx); % extract data for this month
disp(['Avg min temp for ',datestr([1950,k,1,0,0,0],'mmmm yyyy'),...
' is ',num2str(mean(mintemps_thismonth))])
end

Walter Roberson
Walter Roberson on 18 Feb 2011
Monthly averages is fairly simple using accumarray. See this prior cssm discussion

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!