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

조회 수: 5 (최근 30일)
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
dpb
dpb 2022년 9월 30일
편집: dpb 2022년 9월 30일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

per isakson
per isakson 2022년 9월 30일
편집: per isakson 2022년 10월 1일
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
per isakson
per isakson 2022년 10월 1일
편집: per isakson 2022년 10월 1일
@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 2022년 10월 1일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2022년 9월 30일
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'

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by