retrieve data specific date

6 views (last 30 days)
Den
Den on 7 Jun 2022
Answered: Peter Perkins on 13 Jun 2022
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

Accepted Answer

KSSV
KSSV on 7 Jun 2022
You need to load the nc data into MATLAB. For this you need to use ncdisp and ncread.
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)
ans = 'datetime'
% 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)
ans = 10×1 datetime array
01-Jan-2022 02-Jan-2022 03-Jan-2022 04-Jan-2022 05-Jan-2022 06-Jan-2022 07-Jan-2022 08-Jan-2022 09-Jan-2022 10-Jan-2022
idx2 = t.Day > 10 & t.Day <=20 ;
t(idx2)
ans = 10×1 datetime array
11-Jan-2022 12-Jan-2022 13-Jan-2022 14-Jan-2022 15-Jan-2022 16-Jan-2022 17-Jan-2022 18-Jan-2022 19-Jan-2022 20-Jan-2022
Answer 2:
idx3 = thedates.Day == 1 ;
thedates(idx3)
ans = 12×1 datetime array
01-Jan-2022 01-Feb-2022 01-Mar-2022 01-Apr-2022 01-May-2022 01-Jun-2022 01-Jul-2022 01-Aug-2022 01-Sep-2022 01-Oct-2022 01-Nov-2022 01-Dec-2022
Answer 3:
idx4 = thedates.Month == 3 & thedates.Day == 4 ;
thedates(idx4)
ans = datetime
04-Mar-2022
Like above, you can get the indices. The same indices should be used to extract the data.
  2 Comments
Peter Perkins
Peter Perkins on 13 Jun 2022
Den, KSSV is correct in recommending datetime, but DON'T read about datenum and datestr. They are no longer recommended.

Sign in to comment.

More Answers (1)

Peter Perkins
Peter Perkins on 13 Jun 2022
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]'}

Community Treasure Hunt

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

Start Hunting!

Translated by