Effacer les filtres
Effacer les filtres

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

4 vues (au cours des 30 derniers jours)
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 commentaires
Walter Roberson
Walter Roberson le 30 Sep 2022
You would writelines() instead of writematrix()
dpb le 30 Sep 2022
Modifié(e) : dpb le 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.

Connectez-vous pour commenter.

Réponse acceptée

per isakson
per isakson le 30 Sep 2022
Modifié(e) : per isakson le 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.
ans = '01-Oct-2022 05:17:17'
ans = datetime
01-Oct-2022 05:17:18
double_scalar = datenum(now);
ans = '01-Oct-2022 05:17:18'
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'
  3 commentaires
per isakson
per isakson le 1 Oct 2022
Modifié(e) : per isakson le 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 le 1 Oct 2022
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.

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 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:
ans = '30-Sep-2022 13:39:24'
ans = '30-Sep-2022 13:39:24'
  1 commentaire
Image Analyst
Image Analyst le 30 Sep 2022
Unless you have hundreds of files, this way will be faster than writing and debugging a program to do it.

Connectez-vous pour commenter.


En savoir plus sur Dates and Time dans Help Center et File Exchange




Community Treasure Hunt

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

Start Hunting!

Translated by