how to print datetime in fprintf

200 views (last 30 days)
Alex
Alex on 4 Apr 2023
Edited: dpb on 6 Apr 2023
Hi there,
I have elevation profiles on different days which are stored in datenum, and as want to print out the last measurement and the day that the sample was taken.
I currently have this in a loop , but i keep getting this error :
Error using fprintf Conversion to int64 from datetime is not possible.
fprintf('last row for profile %s on the %s is : %d',ProfName,ProfDateNum,indNaN) % Print out the index of the profile
It will only print out the first date and not the others, any others.
Do you have any suggestions to resplve this please?
Many thanks !

Accepted Answer

dpb
dpb on 4 Apr 2023
Edited: dpb on 6 Apr 2023
We don't have sufficient data to reproduce the problem -- we don't have definitions of ProfName, ProfDateNum, nor indNaN
Guessing
ProfName="Profile XYZ";
ProfDateNum=datetime(now,'convertfrom','datenum');
indNaN=1234;
fprintf('last row for profile %s on the %s is : %d',ProfName,ProfDateNum,indNaN)
last row for profile Profile XYZ on the 04-Apr-2023 16:07:51 is : 1234
We don't get any errors; fprintf is capable of handling a datetime.
So, what's going on? With the statement the above is in a loop and it only does one, one can then add to the guess that the above may be arrays and you forgot to index into them, in which case MATLAB will try to send each array to the format string for output...
ProfName=["Profile ABC";"Profile XYZ"];
ProfDateNum=[datetime(now,'convertfrom','datenum');datetime(now+1,'convertfrom','datenum')];
indNaN=[0123;1234];
try % Put in try..catch block so can proceed past the error
fprintf('last row for profile %s on the %s is : %d',ProfName,ProfDateNum,indNaN)
catch % Show the error that occurred is what got above...
lasterr
end
last row for profile Profile ABC on the Profile XYZ is :
ans =
'Error using fprintf Conversion to int64 from datetime is not possible.'
And, Boom! the error message does show up...ergo, the answer is, when looping over a set of values, don't forget to index into the array...
ProfName=["Profile ABC";"Profile XYZ"];
ProfDateNum=[datetime(now,'convertfrom','datenum');datetime(now+1,'convertfrom','datenum')];
indNaN=[0123;1234];
for i=1:numel(ProfName)
fprintf('last row for profile %s on the %s is : %d',ProfName(i),ProfDateNum(i),indNaN(i))
end
last row for profile Profile ABC on the 04-Apr-2023 16:07:51 is : 123last row for profile Profile XYZ on the 05-Apr-2023 16:07:51 is : 1234
Now what shows up is that you forgot to include a newline so everything is all run together...
fmt='last row for profile %s on the %s is : %d\n'; % put the format string in variable for ease edit
for i=1:numel(ProfName)
fprintf(fmt,ProfName(i),ProfDateNum(i),indNaN(i))
end
last row for profile Profile ABC on the 04-Apr-2023 16:07:51 is : 123 last row for profile Profile XYZ on the 05-Apr-2023 16:07:51 is : 1234
You can control the format of the date by setting the .Format property of ProfDateNum. As a stylistic comment only, since it really is a datetime variable, using "DateNum" in the variable name is somewhat misleading and may lead to misuse later on when don't remember so clearly and interpret the variable name as being what it says it is.
  4 Comments
Alex
Alex on 6 Apr 2023
Hi all ,
I got it working ! thank you !

Sign in to comment.

More Answers (1)

Bora Eryilmaz
Bora Eryilmaz on 4 Apr 2023
Edited: Bora Eryilmaz on 4 Apr 2023
You can convert datenum data to datetime data and set its format to the one you desire:
d = datenum(2023, 4, 13);
dt = datetime(d, 'ConvertFrom', 'datenum');
dt.Format = 'MMMM d, yyyy - HH:mm:ss';
fprintf('%s', dt)
April 13, 2023 - 00:00:00
  1 Comment
dpb
dpb on 4 Apr 2023
But, read the error OP got carefully, @Bora Eryilmaz --
Error using fprintf Conversion to int64 from datetime is not possible.
The date variable actually is a datetime despite the ill-chosen name that indicates it is a datenum
The problem is passing arrays to a format string instead of each element of the array in turn in the loop...

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!