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

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)

Str = '155313.894';
Converted1 = sprintf('%c%c:%c%c:%s', Str)
Or:
Converted2 = [Str(1:2), ':', Str(3:4), ':', Str(5:10)]

2 Comments

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
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.

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

Tags

Asked:

on 17 May 2011

Community Treasure Hunt

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

Start Hunting!