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 Center および File ExchangeTables についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by