retrieve data specific date

조회 수: 14 (최근 30일)
Den
Den 2022년 6월 7일
답변: Peter Perkins 2022년 6월 13일
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

채택된 답변

KSSV
KSSV 2022년 6월 7일
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
Den
Den 2022년 6월 10일
thank you so much, such a big help
Peter Perkins
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
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]'}

카테고리

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