retrieve data specific date
10 次查看(过去 30 天)
显示 更早的评论
hello, i'm very new in matlab.
I have 2000-2003 hourly nc files.
I have 3 questions.
1. I want to retrieve data per 10 days each month
d1= 1,2,..,10
d2=11,12,..,20
d3=21,22,..31
2. like the first question but I want data in 1 year, I mean
data=[d1 jan, d1 feb,..,d1 dec]
3. I want to retrieve data only 1 time. For example, March 4, 2002 at 15.00
here I include a bit of my nc file.
thanks for the help
0 个评论
采纳的回答
KSSV
2022-6-7
You read the dates and convert them into the class datetime. For this you need to read about datetime, datenum and datestr. Once you have dates and data in hand, you can play with the indices and get the data you want.
I will make the datetime data and answer your questions.
Answer 1:
thedates = [datetime(2022,1,1):datetime(2022,12,31)]' ; % this is the datetime data, you need to get from nc files
class(thedates)
% pick each 10 days data for Jan
idx = thedates.Month==1 ;
t = thedates(idx) ; % dates for Jan month alone
idx1 = t.Day<=10 ;
t(idx1)
idx2 = t.Day > 10 & t.Day <=20 ;
t(idx2)
Answer 2:
idx3 = thedates.Day == 1 ;
thedates(idx3)
Answer 3:
idx4 = thedates.Month == 3 & thedates.Day == 4 ;
thedates(idx4)
Like above, you can get the indices. The same indices should be used to extract the data.
2 个评论
Peter Perkins
2022-6-13
Den, KSSV is correct in recommending datetime, but DON'T read about datenum and datestr. They are no longer recommended.
更多回答(1 个)
Peter Perkins
2022-6-13
In addition to what KSSV shows, since you say "retrieve data", I recommend that you use a timetable, and things like timerange,or subscripting with datetimes to selec t subsets of your data.
It's possible that some of your uses would benefit from having a categorical variable, not a datetime. For example
>> t = datetime(2022,1,1:365);
>> dayBin = discretize(day(t),[1 11 21 31],"categorical");
>> categories(dayBin)
ans =
3×1 cell array
{'[1, 11)' }
{'[11, 21)'}
{'[21, 31]'}
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!