how to print datetime in fprintf

조회 수: 153 (최근 30일)
Alex
Alex . 2023년 4월 4일
편집: dpb . 2023년 4월 6일
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 !

채택된 답변

dpb
dpb 2023년 4월 4일
편집: dpb 님. 2023년 4월 6일
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
Alex
Alex 2023년 4월 6일
Hi all ,
I got it working ! thank you !

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

추가 답변 (1개)

Bora Eryilmaz
Bora Eryilmaz 2023년 4월 4일
편집: Bora Eryilmaz 님. 2023년 4월 4일
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
dpb
dpb 2023년 4월 4일
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...

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by