How to store time data separated by colon (e.g 15:59:51:111) as a single data point in a matrix?

6 vues (au cours des 30 derniers jours)
Imagine you collected some data on your location vs time. This data is presented as a line of numbers as follows: Time1, Latitude1, Longitude1, Time2, Latitude2,Longitude2.... How to make a matrix out of this data if every "time" point is stored as "h : min : sec" separated by colon. In other words, how to prevent matlab from attempting to create a vector out of time units separated by colon?
Example of data:
time,Latitude,Longitude,
15:59:51:111,25.43734933,55.49849743,
15:59:52:098,25.43705392,55.49978615,
15:59:53:098,25.43645533,55.50090108,

Réponse acceptée

Walter Roberson
Walter Roberson le 14 Août 2022
Assuming that you are reading from a file
%overhead to get the data into a file
S = [
"time,Latitude,Longitude,"
"15:59:51:111,25.43734933,55.49849743,"
"15:59:52:098,25.43705392,55.49978615,"
"15:59:53:098,25.43645533,55.50090108,"
];
filename = tempname;
writelines(S, filename);
%actual work
opt = detectImportOptions(filename);
opt = setvartype(opt, 'time', 'string');
T = readtable(filename, opt);
modtime = regexprep(T.time, ':(\d\d\d)$', '.$1');
T.time = duration(modtime)
T = 3×3 table
time Latitude Longitude ________ ________ _________ 15:59:51 25.437 55.498 15:59:52 25.437 55.5 15:59:53 25.436 55.501
If you have the data in some other form, not in a file, then we would have to know how it is represented now in order to advise you.
  4 commentaires
Kirill Kurzanov
Kirill Kurzanov le 16 Août 2022
Thank you for your quick response. I have attached the requested file below.
Walter Roberson
Walter Roberson le 16 Août 2022
Modifié(e) : Walter Roberson le 16 Août 2022
The below code is tested for that particular input file, which has a blank line at the beginning. If you were using other files that did not have the leading blank line, you would not use 'headerlines', 1
filename = 'Sample.uu';
opt = detectImportOptions(filename, 'Filetype', 'text', 'headerlines', 1);
opt = setvartype(opt, 'time', 'string');
T = readtable(filename, opt);
modtime = regexprep(T.time, ':(\d\d\d)$', '.$1');
T.time = duration(modtime);

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 14 Août 2022
Use a table instead of a matrix:
Time = datetime({'15:59:51:111'; '15:59:52:098'; '15:59:53:098'}, ...
'InputFormat', 'HH:mm:ss:SSS', 'Format', 'HH:mm:ss.SSS');
Data = [25.43734933,55.49849743; ...
25.43705392,55.49978615; ...
25.43645533,55.50090108];
T = table(Time, Data(:, 1), Data(:, 2))
T = 3×3 table
Time Var2 Var3 ____________ ______ ______ 15:59:51.111 25.437 55.498 15:59:52.098 25.437 55.5 15:59:53.098 25.436 55.501

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by