I use matlab R2015a, when I run my script I want print the date and the time of exection

I tried this code

Dtimes=datetime('now','TimeZone','local','Format','d-MMM-y HH:mm:ss Z');
% the first test
disp(' Date and time is ' Dtimes ); % But not work
% second test
disp(sprintf('date and time is %s  ',Dtimes)) %also not work

But with this command is worked :

disp(sprintf('date and time is %s  ',datestr(now)));

What is the problem?

  • 1
    $\begingroup$ How is that connected with signal processing? $\endgroup$ – jojek Jul 8 '15 at 8:47

While significantly off topic, this one is easy: Dtimes is datetime object and not a string. What would work is

disp([' Date and time is ' char(Dtimes)] ); 

Based on Mathworks' documentation here, datetime returns an array, not a string. Therefore, you cannot use disp to display a string Date and time is together with that returned array.

Your second method is also not working because again Dtimes is an array and you cannot use %s to print it.

Your last method is working because datestr returns a string see documentation.

Please let me know if this explanation is not clear/enough.


Not the answer you're looking for? Browse other questions tagged or ask your own question.