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)
Show older comments
Kirill Kurzanov
on 14 Aug 2022
Edited: Walter Roberson
on 16 Aug 2022
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,
0 Comments
Accepted Answer
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)
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
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);
More Answers (1)
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))
0 Comments
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!