Split Climate Data for month and yers

Hello! I have a problem. I created this table from a station file.txt:
DATE PRCP(mm) TMAX(°C) TMIN(°C)
19300101 0 20 -2
....
20200921 2 15 1
I need to split those data for Months and for Years:
es. months
193001(all days)
...
193101(all days)
es. years
1930 (all days)
...
2020(all days)
The outputs should be two matrices with 4 columns divided for month/year.
I have two ideas but i don't know how to proceed further:
1)convert date to string
2)Difference between consecutives days. (19300102-19300101=1 --> same month)
Can anyone help me? Thanks a lot!

답변 (1개)

Rik
Rik 2020년 9월 21일

0 개 추천

d=19300101;
s=sprintf('%08d',d);
y=str2double(s(1:4));
m=str2double(s(5:6));
d=str2double(s(7:8));
Or even simpler:
d=19300101;
s=sprintf('%08d',d);
datetime(s,'InputFormat','yyyyMMdd')
The second one will even support cellstr input, so you can use arrayfun to convert everything in one fell swoop.

댓글 수: 4

Giacomo Moraglia
Giacomo Moraglia 2020년 9월 21일
편집: Giacomo Moraglia 2020년 9월 21일
Hello Rik, thanks for the answer but this can only be used to convert the format of the date. I don't need this.
Rik
Rik 2020년 9월 21일
How were you planning to select your data based on the date, if you don't have the date in a format Matlab understands? With this solution in hand you can use findgroups, as you can access all the relevant date functions (like month and year).
The last part of your question clearly states you needed a way to determine the date from the numeric format. My answer shows you how to do that. You can use month on the datetime output to get the month number.
What exactly is your question now?
Giacomo Moraglia
Giacomo Moraglia 2020년 9월 21일
Sorry, I am newbie and i did not know the findgroup function...
When i try using your script on the first column it only works for the first day.
The first block of code I posted only works for scalars. For this time it doesn't really matter, but next time please post your code as code, instead of a screenshot.
The line below should work.
date_list=arrayfun(@(d)datetime(sprintf('%08d',d),'InputFormat','yyyyMMdd'),ALL);

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

카테고리

도움말 센터File Exchange에서 Time Series Objects에 대해 자세히 알아보기

태그

질문:

2020년 9월 21일

댓글:

Rik
2020년 9월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by