How to extract the months from a datetime table?

조회 수: 24 (최근 30일)
Ashfaq Ahmed
Ashfaq Ahmed 2023년 2월 27일
편집: Ashfaq Ahmed 2023년 2월 27일
Hi!
I have a series of dates like this -
I want to create 12 variables naming - January, February, March, April,.., December in a way that each of the month contains the index value from the DateStamp for that month.
For example,
1984-05-02 is index 1, and it be assigned under 'May'.
Similarly, 1984-06-03 is index 2, and it be assigned under 'June'. And so on.
I have attached the DateStamp.mat file. Any feedback will be much appreciated! Thank you so much.

채택된 답변

Stephen23
Stephen23 2023년 2월 27일
편집: Stephen23 2023년 2월 27일
S = load('DateStamp.mat')
S = struct with fields:
DateStamp: [764×3 table]
T = S.DateStamp;
T.month = T.date;
T.month.Format = 'MMMM'
T = 764×4 table
date x_CloudCover landsat month __________ ____________ _____________ _________ 1984-05-02 0.12 {'landsat_5'} May 1984-06-03 18.49 {'landsat_5'} June 1984-06-19 0 {'landsat_5'} June 1984-09-23 1.49 {'landsat_5'} September 1984-10-09 47.1 {'landsat_5'} October 1984-10-25 7.27 {'landsat_5'} October 1984-11-10 46.36 {'landsat_5'} November 1984-11-26 0.08 {'landsat_5'} November 1985-01-29 0.11 {'landsat_5'} January 1985-04-19 71.61 {'landsat_5'} April 1985-06-22 1.53 {'landsat_5'} June 1985-08-09 0 {'landsat_5'} August 1985-10-12 0 {'landsat_5'} October 1985-10-28 0 {'landsat_5'} October 1986-01-16 16.37 {'landsat_5'} January 1986-02-01 43.73 {'landsat_5'} February
But I am guessing that your actual goal is to perform some kind on analysis on your data, e.g. the means:
M = categorical(datetime(1,1,1,"Format","MMM")+calmonths(0:11));
T.year = T.date.Year;
T.month = M(T.date.Month).';
S = groupsummary(T,["year","month"],"mean","x_CloudCover");
U = unstack(S,"mean_x_CloudCover","month", "GroupingVariables","year")
U = 39×13 table
year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ____ ______ ______ _____ _____ _____ ______ ______ _____ ______ ______ ______ _____ 1984 NaN NaN NaN NaN 0.12 9.245 NaN NaN 1.49 27.185 23.22 NaN 1985 0.11 NaN NaN 71.61 NaN 1.53 NaN 0 NaN 0 NaN NaN 1986 16.37 43.73 NaN NaN 58.07 32.67 35.15 20.3 0 0 NaN NaN 1987 57.67 1.175 0.02 NaN NaN 3.2 NaN 4.2 0.05 38.1 0 11.9 1988 12.78 NaN NaN 1.35 0.32 1.93 32.89 2.15 0 0 27.99 NaN 1989 0.15 52.31 0 16.26 NaN NaN 39.325 27.73 27.95 0 73.26 45.6 1990 NaN NaN NaN NaN 39.08 NaN 31.6 62.61 2.52 0.57 14.19 2.61 1991 42.97 NaN NaN 0.24 42.42 0.05 18.33 23.24 11.95 11.17 0.01 0.43 1992 0.11 19.39 37.41 44.77 0.06 3.2 18.9 NaN NaN 79.05 0.06 0.145 1993 23.675 4.22 NaN 0.18 2.335 23.44 20.35 0 NaN 7.39 NaN NaN 1994 0.36 0.08 NaN 0.56 4.15 NaN 20.115 7.79 7.91 NaN 0 0.28 1995 3.08 25.135 76.31 48.78 NaN 9.47 3.105 0.06 0 16.29 0.15 NaN 1996 9.98 0 0 57.57 NaN NaN 0.1 57.03 NaN NaN 20.15 NaN 1997 30.96 NaN NaN 0.27 46.88 0.13 51.24 0 4.22 NaN NaN 0.05 1998 0.135 0 0 0 NaN 18.545 47.78 5.71 39.99 0 NaN NaN 1999 20.705 29.365 1.495 73.6 0.32 72.77 9.3875 0 47.315 10.918 11.733 0.21
  댓글 수: 4
Ashfaq Ahmed
Ashfaq Ahmed 2023년 2월 27일
편집: Ashfaq Ahmed 2023년 2월 27일
Hi @Stephen23, That is actually something totally new I learned from your code today. Thank you so much!
I have another data set of 376x268x764 double size. This is essentially the latitude/longitude grid of the temperature list of 764 days. I will have to exract the days based on their months. That's why I was looking for the indices only.
I am only using the date stamps from this DateStamp.mat file.
Ashfaq Ahmed
Ashfaq Ahmed 2023년 2월 27일
편집: Ashfaq Ahmed 2023년 2월 27일
I am using this code to find out the indices -
T = S.DateStamp;
T.month = T.date;
T.month.Format = 'MMMM';
LIST = string(T.month(1:end));
find(LIST=="May")

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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