How do I count the days of week in an hourly data set using datetime function?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Ellyn Gray
el 17 de Oct. de 2016
Comentada: Ellyn Gray
el 17 de Oct. de 2016
I have an hourly meteorological data set where I'm looking for instances of low visibility. Normally, I count how many days this occurred in a given month by creating a filter for the low visibility, finding the date and time this occurred on, then counting the unique days:
dateHourlyTZ = datetime(dateHourlyLocal, 'TimeZone','America/Los_Angeles','Format','d-MMM-y HH:mm:ss Z');
duration = (year(dateHourlyTZ(length(dateHourlyTZ),1))-year(dateHourlyTZ(1,1))+1);
%after interpolation, shift timezones
%%%%%%Filtering for Winter %%%%%%%%
dateDay = day(dateHourlyTZ);
dateMonth = month(dateHourlyTZ); %determine the month of each measurement
dateYear = year(dateHourlyTZ);
mat=zeros(duration,26); %preallocate a matrix the size of duration
for i = 1:duration
%yearFilter=(year(t1)-1)+i;
yearFilterEnd=(year(t1)-1)+i; %finds the months at the end of the year, beginning of winter season
yearFilterBegin=year(t1)+i; %finds the months at the beginning of the year, end of the winter season
%WINTER: creates one season of winter dates
dateWinter=dateHourlyTZ((dateYear==yearFilterEnd & dateMonth>=11) | (dateYear==yearFilterBegin & dateMonth<=3));
winterFilter=ismember(dateHourlyTZ,dateWinter);
rowIndex=find(winterFilter); %key to getting exact months of winter season
%visibility between November-March
winterVis=interVis(rowIndex);
f1 = find(~isnan(winterVis)); %find all the values that are not isnans
%analyze individual months
dateMonthWin = month(dateWinter);
%NOVEMBER
novVis=winterVis(novRowIndex); %find the vis that corresponds with those hours
f2 = find(~isnan(novVis)); %identify values that are not NaNs
novLowVisFilter = (novVis <= 0.25); %filters vis below .25 miles
novLowVisHourlySum = sum(novLowVisFilter); %number of low Vis hours in November
rowIndexNovLowVis = find(novLowVisFilter);
dateNovLowVis = novDateWinter(rowIndexNovLowVis); %dates that pertain to low vis
novLowVisDayFilter=unique(day(dateNovLowVis)); %days where vis falls below .25 for one hour or more
novLowVisDaySum=length(novLowVisDayFilter); %number of those days in a month
It goes on and on like this for all the winter months in all the years I have data for. The datetime function has been super useful! And in fact, it's not hard to find the day of the week for each of these. For instance, if I write the following:
dayofweek=day(dateNovLowVis,'name');
I then have a list of all the days of the week. However, these are for hourly measurements, so there are many repetitive days. Here are all the instances of low visibility:
'13-Nov-1994 15:00:00 -0800'
'13-Nov-1994 16:00:00 -0800'
'13-Nov-1994 17:00:00 -0800'
'13-Nov-1994 18:00:00 -0800'
'14-Nov-1994 17:00:00 -0800'
'14-Nov-1994 18:00:00 -0800'
'30-Nov-1994 15:00:00 -0800'
'30-Nov-1994 16:00:00 -0800'
'30-Nov-1994 17:00:00 -0800'
'30-Nov-1994 18:00:00 -0800'
'30-Nov-1994 19:00:00 -0800'
'30-Nov-1994 20:00:00 -0800'
So it spits out the following list of days of the week:
dayofweek =
'Sunday'
'Sunday'
'Sunday'
'Sunday'
'Monday'
'Monday'
'Wednesday'
'Wednesday'
'Wednesday'
'Wednesday'
'Wednesday'
'Wednesday'
I only want each day to be counted once! This seems like it should be an easy fix. For instance, I tried shortening dateNovLowVis so it only included the date, not the time. I thought I could use the unique function to just find unique days and then generate lists of those days of the week, but the time information seems to be embedded in it.
I do have a list of the low vis days of the week for this month (Nov 1994):
13
14
30
But when it outputs this double of dates, I can't use any of the datetime functions on it anymore. I've considered either making a rowIndex to find where these values are, then using datetime and getting the day of the week OR just adding the month and year to each value, and then using datetime. But both those ideas seem very inefficient. Does anyone have a better thought?
I've included my script and an example of the data. I'm sure the whole thing is embarrassingly inefficient, but I am still pretty new to this! Thank you in advance for your help.
0 comentarios
Respuesta aceptada
Peter Perkins
el 17 de Oct. de 2016
Use dateshift, something like this:
uniqueDays = unique(dateshift(dateNovLowVis,'start','day'))
Then extract whatever components you need.
When you say, "I tried shortening dateNovLowVis so it only included the date, not the time.", it sounds like you're changing the display format. That has no effect at all on the actual values, only how they are displayed. Analogously, setting format bank in the command window doesn't truncate all your numeric values to two decimal digits, it just truncated the display.
Más respuestas (0)
Ver también
Categorías
Más información sobre Calendar en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!