Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

matching time format in a cell array

0 views
Skip to first unread message

enviro

unread,
Jan 2, 2010, 9:09:06 AM1/2/10
to
Hi there,
I wanted to match format of a cell array as part of my greater programming. Question is how to add time to particular cells. For instance please see the below array as in put and the required output. In this case, I wanted to add time of 24:00:00 to the date that has no time.


input={
'2006/06/11 20:00:00'
'2006/06/11 21:00:00'
'2006/06/11 22:00:00'
'2006/06/11 23:00:00'
'2006/07/11'
'2006/07/11 01:00:00'
'2006/07/11 02:00:00'
'2006/07/11 03:00:00'
}

output={
'2006/06/11 20:00:00'
'2006/06/11 21:00:00'
'2006/06/11 22:00:00'
'2006/06/11 23:00:00'
'2006/07/11 24:00:00'
'2006/07/11 01:00:00'
'2006/07/11 02:00:00'
'2006/07/11 03:00:00'
};

dpb

unread,
Jan 2, 2010, 9:24:32 AM1/2/10
to
enviro wrote:
...
> ...Question is how to add time to particular cells. For

> instance please see the below array as in put and the required output.
> In this case, I wanted to add time of 24:00:00 to the date that has no time.

...

help datenum
help datestr

--

Oleg Komarov

unread,
Jan 2, 2010, 10:37:03 AM1/2/10
to
See also datevec, addtodate (and hour if you have financial toolbox)

Oleg

enviro

unread,
Jan 3, 2010, 1:23:01 AM1/3/10
to
I think the hour can help here but there are errors with that! Any help please

for i=1:length(input);
if hour (input(i))==0;
input(i)=[input(i),24:00:00];
end
end

dpb

unread,
Jan 3, 2010, 1:34:06 AM1/3/10
to

>> help hour

hour.m not found.

>>

I'd convert to datenum, find those w/o a fractional part and decide what
that part should be; add it and then reconvert...

--

enviro

unread,
Jan 3, 2010, 2:15:02 AM1/3/10
to
Could you please let me know the your datenum code. Please note that the input is an example and cells with no time are different within the cell array!
hour.m code is as below:

function h = hour(d)
%HOUR Hour of date or time.
% H = HOUR(D) returns the hour of the day given a serial date number or
% a date string, D.
%
% For example, h = hour(728647.5590548427) or
% h = hour('19-Dec-1994, 13:24:08.17') returns h = 13.
%
% See also DATEVEC, SECOND, MINUTE.

% Author(s): C.F. Garvin, 2-23-95
% Copyright 1995-2002 The MathWorks, Inc.
% $Revision: 1.6 $ $Date: 2002/04/14 21:51:06 $

if nargin < 1
error('Please enter D.')
end
if isstr(d)
d = datenum(d);
end
c = datevec(d(:)); % Generate date vector from date
h = c(:,4); % Extract hour
if ~isstr(d)
h = reshape(h,size(d));
end

Jan Simon

unread,
Jan 3, 2010, 7:51:03 AM1/3/10
to
Dear enviro!

> I wanted to match format of a cell array as part of my greater programming. Question is how to add time to particular cells. For instance please see the below array as in put and the required output. In this case, I wanted to add time of 24:00:00 to the date that has no time.
>
>
> input={

> '2006/06/11 23:00:00'
> '2006/07/11'
> '2006/07/11 01:00:00'
> }
>

> output={


> '2006/06/11 23:00:00'
> '2006/07/11 24:00:00'
> '2006/07/11 01:00:00'

> };

Are you sure, that 24:00:00 is the right choice? The example looks like 00:00:00 would be better. DATESTR immediately converts '01-Jan-2010 24:00:00' to '02-Jan-2010 00:00:00'!

A more trivial approach is appending the time to all strings, which are short:
output = input;
short = cellfun('length', output) < 19;
output(short) = strcat(output(short), {' 24:00:00'})

If you use ' 24:00:00' as string without { and }, the leading space disappears.

Good luck, Jan

us

unread,
Jan 3, 2010, 9:55:05 AM1/3/10
to
enviro <farhadne...@yahoo.co.uk> wrote in message <914214837.4659.126244...@gallium.mathforum.org>...

one of the many solutions
- needs at least one daytime...

% the data
fmt='yyyy/mm/dd HH:MM:SS';
d={
'2006/06/11'
'2006/06/11'
'2006/06/11'


'2006/06/11 23:00:00'
'2006/07/11'

'2006/07/11'
'2006/07/11'
'2006/07/11'
};
% the engine
dv=datevec(d);
dn=datenum(d);
dx=find(dn-fix(dn)>0,1,'first');
dt=rem(dv(dx,4)+(1:size(d,1))-dx,24);
dv(:,4)=dt.';
r=datestr(dv,fmt);
% the result
disp(r);
%{


2006/06/11 20:00:00
2006/06/11 21:00:00
2006/06/11 22:00:00
2006/06/11 23:00:00

2006/07/11 00:00:00 % <- note proper wraparound


2006/07/11 01:00:00
2006/07/11 02:00:00
2006/07/11 03:00:00

%}

us

0 new messages