Split Climate Data for month and yers

2 次查看(过去 30 天)
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
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
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.
Rik
Rik 2020-9-21
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);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by