How can I programmatically change all my matlab code to use "datetime" rather than "datestr(now)" ?
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Marta
el 29 de Sept. de 2022
Comentada: dpb
el 1 de Oct. de 2022
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 comentarios
dpb
el 30 de Sept. de 2022
Editada: dpb
el 30 de Sept. de 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.
Respuesta aceptada
per isakson
el 30 de Sept. de 2022
Editada: per isakson
el 1 de Oct. de 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)
datetime('now')
double_scalar = datenum(now);
datestr(double_scalar)
datetime(double_scalar)
3 comentarios
per isakson
el 1 de Oct. de 2022
Editada: per isakson
el 1 de Oct. de 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
el 1 de Oct. de 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
Más respuestas (1)
Image Analyst
el 30 de Sept. de 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)
char(datetime)
1 comentario
Image Analyst
el 30 de Sept. de 2022
Unless you have hundreds of files, this way will be faster than writing and debugging a program to do it.
Ver también
Categorías
Más información sobre Data Import and Analysis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!