Input data file containing dashes, colon and slashes

11 vues (au cours des 30 derniers jours)
b
b le 12 Mai 2020
Commenté : b le 13 Mai 2020
I have a data file which contains data in the following format :
2020-Mar-08 02:28 09 46 28.67 +17 21 52.5 -12.22 3.95 0.00242027652616 -0.0482121 155.1431 /T 24.7981
2020-Mar-09 00:00 10 45 35.20 +12 42 10.2 +12.58 3.65 0.00239787063143 -0.0287020 168.5361 /T 11.4363
2020-Mar-10 23:34 11 43 12.33 -07 10 19.6 -12.73 3.52 0.00238767904556 -0.0063201 173.8507 /L 6.1347
2020-Mar-11 19:21 12 39 32.82 +01 11 06.2 -12.40 3.82 0.00239061880814 0.0163142 161.3156 /L 18.6406
2020-Mar-12 12:07 13 35 08.81 -04 49 28.4 +12.03 4.12 0.00240606204543 0.0365759 147.4799 /L 32.4459
There is a space at the very first column (before all 2020s). There are five spaces between the time and the next quantity and three spaces between the 9th and 10th quantity. The sixth quantity always has a + or a - sign, but the 12th quantity does not contain a + sign when it is above 0.
How to read this file for the dates to go in one vector, the times in another vector (without the colon) and the rest of the quantities in separate vectors ? The /T needs to be replaced with digit '1' and /L with digit '2'.
  1 commentaire
Rik
Rik le 12 Mai 2020
What have you tried? It is easier to improve slow working code than to come up with new code.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 12 Mai 2020
filename = 'data.txt';
opt = detectImportOptions(filename);
opt.VariableTypes{2} = 'duration';
opt.VariableOptions(2).InputFormat='hh:mm';
t = readtable(filename, opt);
dates = t.Var1;
times = t.Var2;
[and keep going with the other .Var up to 15]
[But really it is better to leave everything in the table]
[~, TL] = ismember(t.Var14, {'/T', '/L'}); %TL will be 1 or 2, or 0 if not found
  4 commentaires
Walter Roberson
Walter Roberson le 12 Mai 2020
times = hours(t.Var2)
However, in my experience it is much more common to want to skip that and to instead go directly to
datetimes = t.Var1 + t.Var2;
datetimes.Format = 'uuuu-MMM-dd hh:mm';
b
b le 13 Mai 2020
Many thanks ! Completely works !

Connectez-vous pour commenter.

Plus de réponses (1)

b
b le 12 Mai 2020
Huh, What happened ??!!
Where did Stephen Cobeldick's code snippet vanish ??!
Stephen - after putting the time specifier as %{hh:mm}T, the code snippet that you suggested gave a very strange result. The time comes correct, but all the rows where the 14th variable is /T are ignored and the table only stores the rows where the value of this variable is /L. So in this case, the table only reads in the bottom three rows, and ignores the top two rows.
Incidentally, how can we remove the colon from the time data so that it can be stored in the decimal format ?

Catégories

En savoir plus sur Dates and Time dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by