time format as hr:min:sec

1 view (last 30 days)
seema niran
seema niran on 23 Jan 2017
Answered: Peter Perkins on 6 Feb 2017
sir, i have three columns- one containing hour, next minute , third on sec.
hr=[10,10,10,10];
min=[00,00,01,01];
sec=[00,30,00,30];
i need a single column of time as
[10:00:00,10:00:30,10:01:00,10:01:30]
what is the method?

Accepted Answer

KSSV
KSSV on 23 Jan 2017
hr=[10,10,10,10];
min=[00,00,01,01];
sec=[00,30,00,30] ;
iwant = [] ;
for i = 1:length(hr)
str = sprintf('%02d:%02d:%02d\n',hr(i),min(i),sec(i)) ;
iwant = [iwant ; str] ;
end
iwant
  8 Comments
seema niran
seema niran on 6 Feb 2017
sir, it is correct. the 707th term in data was a negetive one making all the problem. thanks
Stephen23
Stephen23 on 6 Feb 2017
@seema niran: note that my Answer works with negative values as well, without error, as well as being simpler and much more efficient that the answer you accepted.

Sign in to comment.

More Answers (2)

Stephen23
Stephen23 on 23 Jan 2017
Edited: Stephen23 on 23 Jan 2017
>> hr=[10,10,10,10];
>> min=[00,00,01,01];
>> sec=[00,30,00,30];
>> fprintf('%02d:%02d:%02d\n',[hr;min;sec])
10:00:00
10:00:30
10:01:00
Or to put it into a string or cell array:
>> S = sprintf('%02d:%02d:%02d\n',[hr;min;sec])
S =
10:00:00
10:00:30
10:01:00
>> C = strsplit(strtrim(S),'\n')'
C =
'10:00:00'
'10:00:30'
'10:01:00'
'10:01:30'

Peter Perkins
Peter Perkins on 6 Feb 2017
All the previous answers assume you want text, and that may be what you want. But if you're using R2014b or later, you can use durations:
>> hr=[10,10,10,10];
>> min=[00,00,01,01];
>> sec=[00,30,00,30];
>> d = duration(hr,min,sec)
d =
10:00:00 10:00:30 10:01:00 10:01:30

Categories

Find more on Creating and Concatenating Matrices 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!