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

2 views (last 30 days)
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,

Accepted Answer

Walter Roberson
Walter Roberson on 14 Aug 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 Comments
Walter Roberson
Walter Roberson on 16 Aug 2022
Edited: Walter Roberson on 16 Aug 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);

Sign in to comment.

More Answers (1)

Jan
Jan on 14 Aug 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

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!