How to get string that uses datetime Format property?

조회 수: 4 (최근 30일)
Bruce Elliott
Bruce Elliott 2015년 9월 28일
댓글: Bruce Elliott 2015년 9월 28일
Note: I posted this question to the newsgroup as well. I'm not sure which is the more appropriate forum.
Is there a way to get string variable that is equivalent to the output of the disp method for a datetime object, which is written to the command line interface? I'd like to print formatted strings from datetime arrays to a file, but fprintf says that it is not defined for datetime inputs.
  댓글 수: 1
Guillaume
Guillaume 2015년 9월 28일
forum and newsgroup are as appropriate. Use whichever you prefer or both.

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

채택된 답변

Guillaume
Guillaume 2015년 9월 28일
The way to convert a datetime into a string according to the datetime Format attribute is actually very simple: Use char
d = datetime;
s1 = char(d)
d.Format = 'uuuu-MM-dd''T''HH:mm:ss';
s2 = char(d)
The documentation of datetime (accessed through doc datetime) is actually not very good as it does not tell you all the methods of the datetime object. However you can see all of them with
help datetime
  댓글 수: 1
Bruce Elliott
Bruce Elliott 2015년 9월 28일
Thank you, Guillaume! I had thought there must be an easy way to do this, since the formatted strings are clearly available. Thanks also for the tip to check the help text for datetime - I had been relying heavily on the documentation since I'm still learning about the datetime class.

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

추가 답변 (1개)

Peter Perkins
Peter Perkins 2015년 9월 28일
Bruce, in your C.S-S.M post, you said, "write data and labels to a file". It's worth noting that if you have everything in a table, writetable will automatically write out the datetimes to the file with no conversion necessary. It uses the format and the timezone that the datetime variable in the table is set to, so if you want both a local and a UTC timestamp in the file, you'd have to have two datetime variables in the table.
  댓글 수: 1
Bruce Elliott
Bruce Elliott 2015년 9월 28일
Thanks, Peter. That sounds like it's worth a try, although I'm currently assembling the table line-by-line and just writing each line to the file as I go. Still, if I can figure out how to collect all the necessary data into the table and then write it all at once, that would be a lot simpler.

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by