how to use datetime(datestr)

Dear all, I tried to convert the matlab serial dates (vector) back to a date string . I use matlab2016b. The data and codein the attached file.
And the error is:
Could not recognize the format of the date/time text. You can specify a format character vector using the 'InputFormat' parameter.
If the date/time text contain day, month, or time zone names in a language foreign to the 'zh_CN' locale, those might not be
recognized. You can specify a different locale using the 'Locale' parameter.
Error in test (line 2)
Time = datetime(datestr((DATA(:,1)+datenum('12/31/1899','mm/dd/yy'))));time_num is a vector of serial dates. I have attached the file please.
Thank you very much!

Réponses (1)

Walter Roberson
Walter Roberson le 9 Août 2020

0 votes

Do not try to mix serial data numbers and datetime objects.
You can skip most of your problems.
t = readtable('data.xlsx');
Time = t{:,1};
Time will already be in datetime format, having been automatically converted from Excel date format.

8 commentaires

zhize lee
zhize lee le 9 Août 2020
Thank you very much. But how can fix it without the applied error.
That is the code. No datestr() or anything like that are needed. After the two lines I gave, Time will be a column vector of datetime objects
t =
998×12 table
Var1 CPIAUCSL DSPIC96 INDPRO LNS13023653 PAYEMS PCE PCEPI PCEPILFE UNRATE gz_spread ebp
___________ ________ _______ ________ ___________ ______ _______ _______ ________ ______ _________ _______
01-Jan-1947 21.48 NaN 14.0679 NaN 43535 NaN NaN NaN NaN NaN NaN
01-Feb-1947 21.62 NaN 14.151 NaN 43557 NaN NaN NaN NaN NaN NaN
01-Mar-1947 22 NaN 14.2341 NaN 43607 NaN NaN NaN NaN NaN NaN
and so on.
zhize lee
zhize lee le 12 Août 2020
the following code need the datestr()
Walter Roberson
Walter Roberson le 12 Août 2020
The code you posted has two lines. There are no following lines that need datestr() .
Note: there is an error in your date conversion code . All of the dates come out as being on the second day of the month, but they should be on the first day of the month instead.
Excel had a bug in the design of dates, so that as far as it was concerned February 29, 1900 existed -- it thought 1900 was a leap year, when it is not. You have to make a one day correction because of that. readtable() automatically makes the correction.
zhize lee
zhize lee le 12 Août 2020
Thank you very much, can you give me your email, I'll send you the entire code to you.
If later on you do need a text form of the datetime array you can call datestr on it.
d = datetime('now')
s = datestr(d)
zhize lee
zhize lee le 12 Août 2020
but, how can just change the code with the original form.Thank you!
With the posted code, you can just use what I posted already,
t = readtable('data.xlsx');
Time = t{:,1};
You have not posted any code beyond that, and any code beyond that would have to be using datestr() if it wanted datestr because your current code is already generating datetime objects.
can you give me your email
Sorry, I work with code that is posted in public. I do not read most of the code that is sent to me in email; I either let it sit without response, or else I tell the person that they need to post it in public if they want a response from me (unless they want to hire me.)

Connectez-vous pour commenter.

Question posée :

le 9 Août 2020

Commenté :

le 12 Août 2020

Community Treasure Hunt

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

Start Hunting!

Translated by