Hello,
I am new to Matlab and I am trying to spread monthly data into a table that is indexed daily. For example:
Date Data
01/1982 0.15
02/1982 -0.02
03/1982 -0.05
What I would like to achieve...
Date Data
1/1/1982 0.15
1/2/1982 0.15
1/3/1982 0.15
1/4/1982 0.15
....
1/31/1982 0.15
2/1/1982 -0.02
2/2/1982 -0.02
2/3/1982 -0.02
2/4/1982 -0.02
I other words, I am trying to convert a table with 240 rows (20 years in months) to one with 7300 rows (20 years in days).
Is this easily doable in matlab? Greatly appreciate any advice.

 채택된 답변

Kelly Kearney
Kelly Kearney 2021년 9월 25일

2 개 추천

One way to do the expansion... convert your monthly dates to the first of each month, then use dateshift to check which month each of your days match:
% Some placeholder data... you'll have to read your tables as appropriate
tmon = datetime(1982, 1:3, 1);
ymon = [0.15 -0.02 -0.05];
tday = datetime(1982,1,1) + days(0:89);
% Match up days to months
[~,loc] = ismember(dateshift(tday, 'start', 'month'), tmon);
yday = ymon(loc);

댓글 수: 3

Ajay Gupta
Ajay Gupta 2021년 9월 26일
Thank you! It worked but now I have to complicate the problem.
The dateshift method you showed worked for one column of data, but I actually have 1000 columns in my data. I tried just calling the 240x1000 array into the ymon variable and it worked, but only for the first column (output is 1x7300 not 1000x7300 as I was expecting). Is there a quick way to revise this code to have it read all 1000 columns and make the shifted dataset?
Thanks again. This was very helpful.
Kelly Kearney
Kelly Kearney 2021년 9월 27일
Can you clarify how your data is stored? Is it in an external file?
Ajay Gupta
Ajay Gupta 2021년 9월 27일
It is not external. Its an array created by my code. (241x1000 double) trying to get to 7300x1000 double.
I think I was able to resolve by adding:
yday = zeros(7300,1000);
yday(:,:) = ymon(loc,:);
Does this make sense?
Thanks again!

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

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2021년 9월 24일

1 개 추천

You can import your data and easily convert them as you need using importdata() - see help DOC and convert date data into numbers using datenum() - see help DOC
Good luck

카테고리

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

질문:

2021년 9월 24일

댓글:

2021년 9월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by