Add colon in between 6 digit numbers to represent time

4 views (last 30 days)
Hi guys,
Hopefully this is an easy one for some but can't figure out a way myself. I have 6 digit numbers and want to add colons in between every two digits, to represent time.
For example, I have 134911 and want to make it 13:49:11 (in hh:mm:ss), which will allow me to estimate time intervals for some processing I perform later on.
Any help, would be much appreciated!.
Thank you
  3 Comments
George Papas
George Papas on 2 Feb 2019
Edited: George Papas on 2 Feb 2019
Hi Stephen,
Thanks for this. This is stored in a Matlab function where I produce a standard numeric vector, after I use the dicominfo command:
info=dicominfo(fullfile(Pathname,Filename{i}));
Time(i)=str2num(info.AcquisitionTime);
I am attaching an example.
Stephen23
Stephen23 on 3 Feb 2019
Storing this in a single integer misrepresents the information stored in a date string. It is easier to parse this character vector/string directly:
>> info.AcquisitionTime = '134911';
„>> sprintf('%c%c:%c%c:%c%c',info.AcquisitionTime)
ans = 13:49:11

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 2 Feb 2019
One approach:
Time = 134911;
Tr = [fix(Time/1E+4) fix(Time/1E+2)-1E+2*fix(Time/1E+4) rem(Time, 1E+2)];
Tstr = sprintf('%02d:%02d:%02d', Tr)
producing:
Tstr =
'13:49:11'
There are likely ways to get the date and time functions to do this as well.

More Answers (1)

George Papas
George Papas on 2 Feb 2019
Many thanks, it works!!

Categories

Find more on Dates and Time 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!