Clear Filters
Clear Filters

Question on datestr to datetime conversion

21 views (last 30 days)
Hi everyone, I'm working on a code that is a few years old and not originally made by me. Untill now I used matlab only for specific operation with matrices so I'm sorry if the question appear to be dumb.
First thing I did was to look at all warnings and tried to correct the code to not get them. Now the only one I still need to change is the use of datestr that since then it's become a discouraged instances.
Now I know that we can change:
d = datetime(t)
d = string(d)
or even in a more compact way:
Now what I have in my code is something like this:
and looking at the documentation of both functions I'm not sure on how to change it mantaining the expected output. If possible I would want to change only the datestr lines and I need to be sure that the result is exactly the same and not similar.
If anyone could help thank you very much :)

Accepted Answer

Stephen23 on 11 May 2023
Edited: Stephen23 on 11 May 2023
Most likely TIMESERIES is a serial date number, the use of which is also discouraged and most likely should be replaced with DATETIME objects anyway: the point of DATETIME is not just that it replaces the deprecated functions, but also the various deprecated ways of storing or representing dates. So quite possibly that line would not really be required at all, because the modern approach would be that TIMESERIES should be DATETIME already, and therefore not need further conversion.
But if you want a mixed-up approach using a mix of deprecated serial date numbers and DATETIME, then you would need this:
datetime(timeseries(i), 'ConvertFrom','datenum', 'Format','...notTheSameFormatString...')
However most likely you should import/define TIMESERIES correctly as DATETIME in the first place.
Guido on 16 May 2023
Thanks everyone, after spending a lot of time looking at the code I decide not to change the datestr with datetime because I'm still not sure I will get the exact same output everywhere one the code and also because as Stephen23 point out it would still use a timeseries and I cannot go and rewrite all the previously written function that needs to be compatible with data that are acquired from many different instuments.
Anyway thanks to all for the help and I'll keep these answer in mind if I will ever need/want to rewrite this code from scratch.
Steven Lord
Steven Lord on 16 May 2023
When or if you do refactor the code, I strongly recommend creating tests that lock down the current behavior of your code and the expected correct behavior of the code. [Those are not necessarily the same thing!] This may help you carve off small sections of the code to be reviewed and/or revised while reducing the chances that changes to those sections of code break your larger application.

Sign in to comment.

More Answers (1)

Peter Perkins
Peter Perkins on 5 Jun 2023
It's hard to tell what this code is doing, but while replacing this
with this
is a literal old to new translation, but it may not be what's really needed. The old datestr/datenum/datevec stuff required a lot of converting between representations to variously do arithmetic, calendar arithmetic, and readable output. datetime is intended to mostly avoid those kind of conversions: it's displays in whatever format you want, supports exact-time arithmetic with durations, and supports calendar arithmetic with calendarDuration.
So you may consider doing a less literal translation, and look at your code more conceptually.
Also: "... a custom function we made that create a table with real world data and the date associated with it." Dunno know what that means, but I'm guessing you should look at timetables.


Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!