retrieve data specific date

13 ビュー (過去 30 日間)
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
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 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
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 ;
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 ;
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 ;
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 ;
ans = datetime
Like above, you can get the indices. The same indices should be used to extract the data.
  2 件のコメント
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 Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by