readtable interpretes time hh:mm:ss:SSS in different ways

3 views (last 30 days)
Matlab R2020a:
For reading a text file I use:
buffer = fread(fid, Inf);
fclose(fid);
fid = fopen('BufferFile.txt', 'w');
fwrite(fid, buffer);
R = readtable('BufferFile.txt', "FileType", "text");
fclose(fid);
For BufferFile.txt = A
R will show the seconds with fractional part, see column 2,
for BufferFile.txt = B (containing the seconds with fractionals in the same way)
R shows the seconds as int.
How can I make them to int in both cases?

Accepted Answer

Jan
Jan on 11 May 2021
Edited: Jan on 11 May 2021
I do not understand the meaning of your file operations. As far as I understand, the problem can be reproduced by:
RA = readtable('A_BufferFile.txt', "FileType", "text");
RB = readtable('B_BufferFile.txt', "FileType", "text");
Correct?
Now I get in R2018b:
RA(1:3, :)
3×15 table
x_ GPST latitude_deg_ longitude_deg_ height_m_
____________ ____________ _____________ ______________ _________
'2021/03/09' 16:43:20.000 49.486501109 11.128521899 393.4746
'2021/03/09' 16:43:21.000 49.486500611 11.128522695 393.4638
'2021/03/09' 16:43:22.000 49.486500796 11.128523451 393.4387
% ^^^
RB(1:3, :)
3×15 table
x_ GPST latitude_deg_ longitude_deg_ height_m_
____________ ________ _____________ ______________ _________
'2021/03/08' 15:00:46 49.48639163 11.128834329 387.0342
'2021/03/08' 15:00:47 49.486350345 11.128823205 389.2862
'2021/03/08' 15:00:48 49.486346769 11.128809793 391.7787
The different display comes from fractional parts in RA.GPST, e.g.:
16:53:51.001
17:05:40.001
17:05:41.001
This let readtable modify the Format of the duration automagically:
RA.GPST.Format % 'hh:mm:ss.SSS'
RB.GPST.Format % 'hh:mm:ss'
You can round this and/or modify the format:
RA.GPST = round(RA.GPST)
RA.GPST.Format = 'hh:mm:ss'
Note: This is fragile:
fid = fopen('BufferFile.txt', 'w');
fwrite(fid, buffer);
R = readtable('BufferFile.txt', "FileType", "text");
fclose(fid);
Reading from a file, which is open for writing might work, but this is not guaranteed. Close the file before reading it to flush the buffer.
  3 Comments
Jan
Jan on 12 May 2021
@Walter Roberson: 15 years ago I had trouble with FSEEK on a Linux machine. On very heavy load the operation was not successful. It was a timing problem and I've found some sources, which explains, why the underlying C function fails, when a "thread switch" occurs. At least the replies status was -1 in this cases. After a failed fseek(FID, x, 0) it was impossible to obtain the current position, so fseek(fid, ftell(fid) + 1, -1) was the stable solution.
In Matlab 6.5 opening a file twice changed the read/write access for the other file handle also. This has been changed later, but I decided to reduce the risk of bugs by closing files before they are accessed elesewhere.
Does fseek() flush reliably today even if the file was opened in 'W' or 'A' mode (uppercase)? Is this documented?

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!