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'
};
...
help datenum
help datestr
--
Oleg
for i=1:length(input);
if hour (input(i))==0;
input(i)=[input(i),24:00:00];
end
end
>> 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...
--
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
> 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
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