Converting hhmmss.mmm to hh:mm:ss.mmm, or something like that

4 views (last 30 days)
My array includes a column of time data which is in the format "hhmmss.mmm".
An example is "155313.894", which means 15:53:13.894 (hh:mm:ss.mmm).
I would like to convert this value into a time value that I can use to plot in either Matlab or Excel.
datestr($$$) does not work because there is no date data, only time.
Suggestions? Help?
Thanks!
-adam-

Answers (2)

Jan
Jan on 17 May 2011
Str = '155313.894';
Converted1 = sprintf('%c%c:%c%c:%s', Str)
Or:
Converted2 = [Str(1:2), ':', Str(3:4), ':', Str(5:10)]
  2 Comments
Adam
Adam on 18 May 2011
This helps me change individual values. Thanks.
But I need to do this more broadly so that I can do each number in an array.
My current command...
for a = 1:length(array);
value = ('array(a)');
array(a) = sprintf('%0.3f',array(a));
end
I know that doesn't work, I just can't find it in the Matlab help/tutorial how to move element-by-element through an array...
thanks
Jan
Jan on 18 May 2011
What kind of array do you use? For a cell array:
for a = 1:length(array); array{a} = sprintf('%0.3f', array{a}); end
The original question for a CHAR array:
Converted = strcat(Str(:, 1:2), ':', Str(:, 3:4), ':', Str(:, 5:10)];

Sign in to comment.


Matt Fig
Matt Fig on 17 May 2011
It is not clear to me whether you want a number or another string in a different format. If you want a number, stop after the first line below:
N = datenum('155313.894','HHMMSS.FFF')
S = datestr(N,13) % Pick which format you like. See DATESTR help.

Categories

Find more on Language Support in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!