user2100039
user2100039

Reputation: 1356

Find Date Range Rows and Extract

This should be straight-forward in MATLAB I just don't know how and am stuck. I have data that looks like this below that is in the form: mm/dd/yyyy hh:mm windspeed - this is hourly data spanning years from 1991 up to the present (2013) of cell arrays and numeric array:

8/22/1993 23:00 2.381453514
8/23/1993 0:00  3.39369154
8/23/1993 1:00  5.398661613
8/23/1993 2:00  7.231492996
8/23/1993 3:00  9.187699318
8/23/1993 4:00  9.802619934
8/23/1993 5:00  8.85418129
8/23/1993 6:00  9.889941216
8/23/1993 7:00  10.4628706
8/23/1993 8:00  10.8967123
8/23/1993 9:00  10.12729263
8/23/1993 10:00 9.106620789
8/23/1993 11:00 7.600066185
8/23/1993 12:00 6.597990036
8/23/1993 13:00 6.764455795
8/23/1993 14:00 7.360760689
8/23/1993 15:00 5.828835011

I am trying to extract the third column only (windspeed). I need to be able to change the date range to extract a month at a time for example ALL the rows containing the month "08" for August of the year 1993 and then for future use all the rows containing "09" for September and the year 2013. I'm not sure if it's better to use datenum or the find function and then how to code this for either case.
I am using xlsread to read the .csv file with a portion of the data shown above.

Upvotes: 0

Views: 834

Answers (2)

prgao
prgao

Reputation: 1787

A generic way to read the whole file could be:

fid  = open('data.csv');
% %d = integer, %f = float
data = textscan(fid, '%d/%d/%d %d:%d %f');
fclose(fid);

% all months in data
disp(data{1});

% all wind speeds in data
disp(data{end});

You can restrict the returned data by modifying the matching pattern in textscan:

% only August
mask   = data{1} == 8;
speeds = data{end}(mask);

% June of 1997
mask   = (data{1} == 6) & (data{3} == 1997);
speeds = data{end}(mask);

See more here: http://www.mathworks.com/help/matlab/ref/textscan.html#btg0kes

Upvotes: 1

Stewie Griffin
Stewie Griffin

Reputation: 14939

I don't know how you import your data now, and what your variables in the workspace are, but here's a way to do it:

Find the xxx.csv-file in the Current Folder window in Matlab. Double click it to import it. In the window that appears, you can select Delimiter: Space. Play around with the settings, to find the "best fit"

enter image description here

Now, you have your variable in the Matlab workspace.

hhv = datevec(hh);

% You are interested in column 4, HOUR:
% hhv is a matrix with [2013, 1, 1, HOUR, 0, 0]

Now, if you want all windspeeds between 0:00 and 06:00 08/23/1993:

winds = ws(dt == datenum(1993,8,23) & hhv(:,4) >= 0 & hhv(:,4) < 6)

winds =

    3.3937
    5.3987
    7.2315
    9.1877
    9.8026
    8.8542

Upvotes: 1

Related Questions