How do I include a variable when trying to display a string?

조회 수: 2 (최근 30일)
Omar Abedalrahman
Omar Abedalrahman 2016년 9월 3일
편집: dpb 2016년 9월 3일
I created a start time and saved it. I want to include this variable in the end of the string, but what I'm doing is not working. Here is my current code:
startHW1=datetime('now');
save HW1 startHW1
disp(['I started HW1 on ', num2str(startHW1)]);

채택된 답변

dpb
dpb 2016년 9월 3일
편집: dpb 2016년 9월 3일
Use the optional output format
startHW1=datetime('now','Format','dd-MMM-yyyy HH:mm:ss');
disp(['I started HW1 on ' startHW1])
or, just use now
startHW1=now;
disp(['I started HW1 on ' datestr(startHW1)])
(Addendum/Erratum: the need for datestr here sorta' defeats the purpose; my faux pas explained further in comment to Stephen...)
  댓글 수: 2
Stephen23
Stephen23 2016년 9월 3일
편집: Stephen23 2016년 9월 3일
That second example does not make any sense:
>> startHW1=now;
>> disp(['I started HW1 on ' startHW1])
Warning: Out of range or non-integer values truncated during conversion to character.
I started HW1 on
>>
Why do you want to join character 736576 (or similar) onto the end of that string?
A datestr call is most likely required here...
dpb
dpb 2016년 9월 3일
편집: dpb 2016년 9월 3일
Yeah, at the time was thinking that like date, now returned the string and belatedly realized it this AM...

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by