How can I programmatically change all my matlab code to use "datetime" rather than "datestr(now)" ?
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
dpb
il 30 Set 2022
Modificato: dpb
il 30 Set 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.
Risposta accettata
per isakson
il 30 Set 2022
Modificato: per isakson
il 1 Ott 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 Commenti
per isakson
il 1 Ott 2022
Modificato: per isakson
il 1 Ott 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
il 1 Ott 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
Più risposte (1)
Image Analyst
il 30 Set 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 Commento
Image Analyst
il 30 Set 2022
Unless you have hundreds of files, this way will be faster than writing and debugging a program to do it.
Vedere anche
Categorie
Scopri di più su Data Import and Analysis in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!