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

조회 수: 6 (최근 30일)
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,

채택된 답변

Walter Roberson
Walter Roberson 2022년 8월 14일
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
Kirill Kurzanov
Kirill Kurzanov 2022년 8월 16일
Thank you for your quick response. I have attached the requested file below.
Walter Roberson
Walter Roberson 2022년 8월 16일
편집: Walter Roberson 2022년 8월 16일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jan
Jan 2022년 8월 14일
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

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by