How can I programmatically change all my matlab code to use "datetime" rather than "datestr(now)" ?

5 views (last 30 days)
Matlab R2022b does not recommend using "datestr". I would like to quickly and easily replace that command in all my matlab programs to "datetime". Can I do this programatically?
  4 Comments
dpb
dpb on 30 Sep 2022
Edited: dpb on 30 Sep 2022
writelines wasn't introduced yet in R2020b, Walter...not until R2022a
Will also probably have to use the 'QuoteStrings',0 named parameter to avoid putting quotes around every line in an m-file...and as @per isakson notes, while it may make the substitution, that alone doesn't mean the change is transparent elsewhere. There is only one syntax form in question here, though, but that it will be a datetime instead of character string means it'll probably error when used elsewhere.
One would presume there aren't many of these in much code, however, so it may be just wait and find the then, but I'd also follow his recommendation to only make the switch in new code (or other code that is being actively worked on otherwise, maybe, as well).
grep will let one find instances/locations to go look at though, so one could also build a filter first of m-files containing the idiom.

Sign in to comment.

Accepted Answer

per isakson
per isakson on 30 Sep 2022
Edited: per isakson on 1 Oct 2022
By mistake, I answer the general question: "Can I do this [replace datestr by datetime] programatically?".
IMHO: It cannot be done "quickly and easily". In the documentation of datestr the syntax section takes seven lines. All cases need to be taken care of. Interpret "Not recommended" as a recommedation to use datetime in new code.
The code below illustrates two problems
  • datestr(now) returns a character vector and datetime('now') returns a datetime obj.
  • datestr(double_scalar) returns a character vector and datetime(double_scalar) throws an error.
datestr(now)
ans = '01-Oct-2022 05:17:17'
datetime('now')
ans = datetime
01-Oct-2022 05:17:18
double_scalar = datenum(now);
datestr(double_scalar)
ans = '01-Oct-2022 05:17:18'
datetime(double_scalar)
Error using datetime
Numeric input data must be a matrix with three or six columns, or else three, six, or seven separate numeric arrays. You can also create datetimes from a single numeric array using the 'ConvertFrom'
parameter.
  3 Comments
per isakson
per isakson on 1 Oct 2022
Edited: per isakson on 1 Oct 2022
@Image Analyst Our disagreement is because we put different meaning into the little word "it". datestr(now) is a trivial, I agree. On on the other hand, I read the question as "Can I do this [replace datestr by datetime] programatically?". I focused on the Description and forgot about the Summary - not the first time.
dpb
dpb on 1 Oct 2022
GEEZER ALERT/
Many years ago we were tasked with converting a very large reactor design code from the CDC class machines to the then new-fangled PC environment. This was in the initial AT era, so in fact weren't trying to run on the PC itself but using a coprocessor board with the FORTRAN compiler building code for it. The CDC machine was the 60 bit floating point so the base code was written in single precision while on the 32-bit coprocessor would require double.
We tasked the summer intern to do the conversion of "E+00" and its ilk to "D+00" on some 100K lines of code. Unfortunately, his TECO macro ended up replacing all "E"s with "D"s besides running for some 12 hours on the DEC10 mainframe and buring half the annual computing budget...
Moral. Test automated substitution code VERY carefully before turning it loose in the wild.
/GEEZER ALERT

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 30 Sep 2022
You can use control-shift-f and search for datestr(now).
Unfortunately that multi-file search capability does not (yet) have a replace capability. So for each line of code found, double click on it in the list of hits to bring up that file in the editor.
Now type control H and tell it to replace datestr(now) with char(datetime).
They're the same. Viz:
datestr(now)
ans = '30-Sep-2022 13:39:24'
char(datetime)
ans = '30-Sep-2022 13:39:24'

Categories

Find more on Dates and Time in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!