how to print datetime in fprintf
조회 수: 153 (최근 30일)
표시 이전 댓글
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 :
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 !
댓글 수: 0
채택된 답변
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)
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
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
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
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.
추가 답변 (1개)
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)
댓글 수: 1
dpb
2023년 4월 4일
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 Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!