Datestr() transforms the matrix of days

I have a matrix with the number of days converted using datenum(). Now I want to convert back to the original date, but the result is wrong.The code is as follows:
day=[737433;737445;737457;737469;737481;737493;737505]
value=datestr(day(1:7),'yyyymmdd');
value=str2num(value);
The execution result is:
value=[101;102;103;104;105;106;107];
actually the results should be
[20190108;20190120;20190201;20190213;20190225;20190309;20190321]
How should I solve this problem? Thank you

Answers (1)

I cannot reproduce that. I get what the reults should be (in R2020a):
day=[737433;737445;737457;737469;737481;737493;737505];
value=datestr(day(1:7),'yyyymmdd')
value=str2num(value)
producing:
value =
7×8 char array
'20190108'
'20190120'
'20190201'
'20190213'
'20190225'
'20190309'
'20190321'
and:
value =
20190108
20190120
20190201
20190213
20190225
20190309
20190321
.

5 Comments

I use R2017a, is it the problem of Matlab version?
It works for me in R2017a.
Note by the way that
>> char(101:107)
ans =
'efghijk'
Perhaps that might give you ideas as to where the results came from?
which -all str2num
sun
sun on 9 Sep 2020
Edited: sun on 9 Sep 2020
sir, I inserted some breakpoints for debugging, and found that actually datestr(day(1:7),‘yyyymmdd’) execution result is
[00000101'
'00000102'
'00000103'
'00000104'
'00000105'
'00000106'
'00000107']
At the same time, the execution result in the command line window is correct.
But the execution result in the script is wrong (as mentioned above). What is the reason?
At the time that happens, please show us the content of day(1) and also show us
which -all datestr
Walter — Thank you!

Sign in to comment.

Products

Release

R2017a

Tags

Asked:

sun
on 9 Sep 2020

Edited:

sun
on 9 Sep 2020

Community Treasure Hunt

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

Start Hunting!