Selecting specific time from date and time array?

5 visualizaciones (últimos 30 días)
Seyyed Mohammad Saeed Damadi
Seyyed Mohammad Saeed Damadi el 9 de Jul. de 2018
Respondida: Guillaume el 9 de Jul. de 2018
I have a csv file which has been attached. I have imported the two columns in Matlab cell array using the following codes
fid =fopen('Actual_38.05_-75.45_2006_UPV_61MW_5_Min.csv');
textData = textscan(fid,'%D%f','headerlines',1,'delimiter',',');
fclose(fid)
a = textData{1,1};
b = textData{1,1};
The first column is time from 1/1/2006 to 12/31/2006 sampling each 5 minutes, e.g. (1/1/2006 0:20 and 1/1/2006 0:25). What I want is to select times from 8am to 8pm and its corresponding second column values for all 365 days of year. How can I do it through a loop? I think my first problem is that I cannot find 1/1/2006 8:00 for start. The second problem is I do not know how extract data from 8am to 8pm for day one, i.e. 1/1/2006. The third problem is I do not know how to go to the next day and repeat this process.

Respuestas (2)

Steven Lord
Steven Lord el 9 de Jul. de 2018
Make a datetime vector with times spaced 15 minutes apart.
N = datetime('now');
hours48 = (N-days(1)):minutes(15):(N+days(1));
I'm going to tweak the format used to display the dates and times a bit. This doesn't change the data, just how it is displayed.
hours48.Format = 'dd MMM @ hh:mm:ss a';
Get the hour of the day for each element of the vector.
hoursOfDay = hour(hours48);
Let's find all the elements in hours48 after noon and before 3 PM.
hours48(hoursOfDay >= 12 & hoursOfDay <= 14).'

Guillaume
Guillaume el 9 de Jul. de 2018
Any reason you're not using readtable to read your file. Once read as a table it is trivial to keep only the data you want:
t = readtable('Actual_38.05_-75.45_2006_UPV_61MW_5_Min.csv');
h = hour(t.LocalTime);
t = t(h >= 8 & h < 20, :)
All done!

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by