Selecting specific time from date and time array?

5 次查看(过去 30 天)
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.

回答(2 个)

Steven Lord
Steven Lord 2018-7-9
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 2018-7-9
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!

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by