I have some text files that I want to access them in MATLAB workspace. MATLAB help says that I can use
fscanf
, fgetl
, and textscan
. I chose the last one due to the formatted text.
I wrote the below scripts:
filename = 'myFile.txt';
fid = fopen(filename);
myData = textscan(fid, '%u64 %{dd/MM/yyyy}D %{hh:mm:ss.SSS}T %f64 %f64 %u64 %f64 %f64 %f64\r\n', 'HeaderLines', 3)
fclose(fid);
but I get the error:
Error using textscan
Unable to parse the format character vector at position 21 ==> %{HH:mm:ss.SSS}T %f64 %f64 %u64 %f64 %f64 %f64
Date formats must be of the form %T or %{...}T.
The formatted texts are as:
--------------------------------------------------------------------------------------------------
Row Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8
--------------------------------------------------------------------------------------------------
1 08/04/2018 09:56:52.790 020.00 019.999570 1999690178 055.00 010.020000 000.00000
2 08/04/2018 09:56:52.821 020.00 019.999602 1999690178 055.00 010.020000 000.00000
3 08/04/2018 09:56:52.852 020.00 019.999580 1999690178 055.00 010.020000 000.00000
4 08/04/2018 09:56:52.883 020.00 019.999623 1999690179 055.00 010.020000 000.00000
5 08/04/2018 09:56:52.915 020.00 019.999548 1999690179 055.00 010.020000 000.00000
6 08/04/2018 09:56:52.946 020.00 019.999602 1999690179 055.00 010.020000 000.00000
7 08/04/2018 09:56:52.993 020.00 019.999548 1999690179 055.00 010.020000 000.00000
8 08/04/2018 09:56:53.024 020.00 019.999602 1999690179 055.00 010.020000 000.00000
9 08/04/2018 09:56:53.055 020.00 019.999548 1999690179 055.00 010.020000 000.00000
I always tend to avoid using fscanf
, importdata
, textscan
and such functions because they can be tricky to deal with and I think their output sometimes is not easy to manipulate. On the top of that, your file format looks very similar to the one that Matlab uses for displaying tables data... I think that this is nicenly pointing you to the right direction.
I recommend you to use readtable, not only because of the aforementioned reasons but also because tables are very versatile in Matlab:
T = readtable('data.txt', ...
'Format', '%d %{dd/MM/yyyy}D %{HH:mm:ss.SSS}D %f %f %f %f %f %f', ...
'HeaderLines', 3)
The final output is:
T =
9×9 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9
____ __________ ____________ ____ _________ __________ ____ _____ ____
1 08/04/2018 09:56:52.790 20 19.99957 1999690178 55 10.02 0
2 08/04/2018 09:56:52.821 20 19.999602 1999690178 55 10.02 0
3 08/04/2018 09:56:52.852 20 19.99958 1999690178 55 10.02 0
4 08/04/2018 09:56:52.883 20 19.999623 1999690179 55 10.02 0
5 08/04/2018 09:56:52.915 20 19.999548 1999690179 55 10.02 0
6 08/04/2018 09:56:52.946 20 19.999602 1999690179 55 10.02 0
7 08/04/2018 09:56:52.993 20 19.999548 1999690179 55 10.02 0
8 08/04/2018 09:56:53.024 20 19.999602 1999690179 55 10.02 0
9 08/04/2018 09:56:53.055 20 19.999548 1999690179 55 10.02 0
P.S. = the %{...}T
format is probably due to a misleading way of handling format error messages from the part of Matlab, only %{...}D
is a valid datetime literal format until at least Matlab 2017A.