Convert Number to Time / Duration Format of Data Column
조회 수: 13 (최근 30일)
이전 댓글 표시
Hi, Everyone.... I need your help to convert a series of data column with number format to become duration format. This is my data :
col1 col2 (number) col3 (number)
244 000002 45347.35 29
244 000014 45347.94 19
244 000026 45348.22 69
244 000038 45348.22 69
244 000050 45348.01 69
244 000102 45350.17 09
244 000114 45310.84 09
244 000126 45348.23 79
244 000138 45348.51 69
244 000150 45348.74 69
244 000202 45347.58 19
244 000214 45354.39 09
244 000226 45348.84 69
.....
I just want to convert the whole data in column 2 which re shown as 000002, 000014, ..... 000226 to become time / duration format with matlab script like this :
col2 i want to convert the whole of col 2 to become this of time / duration data
244 000002 - > 00:00:02
244 000014 - > 00:00:14
244 000026 - > 00:00:26
244 000038 - > 00:00:38
244 000050 - > 00:00:50
244 000102 - > 00:01:02
244 000114 - > 00:01:14
244 000126 - > 00:01:26
244 000138 - > 00:01:38
244 000150 - > 00:01:50
244 000202 - > 00:02:02
244 000214 - > 00:02:14
244 000226 - > 00:02:26
Its looks simple, but i cant continue my code after :
[namafile,direktori]=uigetfile({'*.txt', 'Text-files (*.txt)'},'Load Indigo Magnet');
full = fullfile(direktori,namafile);
I want to get the data and convert the column2 to that time / duration data and the data is already attached.... Would you mind to help me in finding the code for that? Thank you very much /.\
댓글 수: 0
채택된 답변
Simon Chan
2021년 7월 18일
Try the following:
clear; clc;
[namafile,direktori]=uigetfile({'*.txt', 'Text-files (*.txt)'},'Load Indigo Magnet');
full = fullfile(direktori,namafile);
file = readcell(full);
B = cellfun(@(x) sprintf('%06d',x),file(:,2),'UniformOutput',false);
B_new = cellfun(@(x) strcat(x(:,1:2),':',x(:,3:4),':',x(:,5:6)),B,'UniformOutput',false);
time = duration(B_new);
추가 답변 (1개)
Peter Perkins
2021년 7월 27일
This is much simpler than you think. You can't currently parse things like "000002" directly into duration, but you can get there via datetime:
>> x = [000002; 000014; 000026];
>> t = datetime(num2str(x,'%06d'),'InputFormat','HHmmss')
t =
3×1 datetime array
27-Jul-2021 00:00:02
27-Jul-2021 00:00:14
27-Jul-2021 00:00:26
>> e = timeofday(t)
e =
3×1 duration array
00:00:02
00:00:14
00:00:26
But more fundamentally, the fact that these are stored as numbers is part of your problem. You don't say how they got there, but you should consider using a tablke for your data, which would allow you to store numbers and text (which 000002 ought to be) in one container. readtable is probably your friend.
Also, numbers like 45347.35 look suspiciously ike Excel serial day numbers:
>> x = [45347.35; 45347.94; 45348.22]
x =
45347
45348
45348
>> t = datetime(x,"ConvertFrom","excel")
t =
3×1 datetime array
25-Feb-2024 08:24:00
25-Feb-2024 22:33:36
26-Feb-2024 05:16:48
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!