MATLAB Answers

Convert Number to Time / Duration Format of Data Column

6 views (last 30 days)
Tyann Hardyn
Tyann Hardyn on 18 Jul 2021
Answered: Peter Perkins on 27 Jul 2021
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 /.\

Accepted Answer

Simon Chan
Simon Chan on 18 Jul 2021
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 Comment
Tyann Hardyn
Tyann Hardyn on 19 Jul 2021
Thank you very much, sir. Now i can plot it to my Gui :

Sign in to comment.

More Answers (1)

Peter Perkins
Peter Perkins on 27 Jul 2021
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

Community Treasure Hunt

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

Start Hunting!

Translated by