Tricky correction of faulty time list

1 view (last 30 days)
Hi. I have a Nx1 cell array containing different time values as strings, mixed wildly. It looks something like this:
12:00
12:05
13:45
09:15
08:25
*08:32*
15:20
*15:26*
but a lot longer... As you can see, the minute values are almost all multiples of 00:05. But some random ones are shifted by one or two minutes.
Is there a way I can run through the array and correct the shifted values? So in my example I would want to change '15:26' to '15:25' and '08:32' to '08:30'.
If it is not possible to automate it, finding them would be also be okay - so that I could display their respective row numbers in a message box to be able to correct them manually.
Thanks in advance for any help!

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 22 Sep 2013
Edited: Azzi Abdelmalek on 23 Sep 2013
v={'12:00';'12:05';'13:45';'09:15';'08:25';'08:32';'15:20';'15:26'}
c=cellfun(@(x) str2double(regexp(x,'(?<=\:).+','match')),v);
c1=round(c/5)*5
for k=1:numel(v)
v{k}=datestr(datenum([v{k}(1:3) num2str(c1(k))],'HH:MM'),'HH:MM');
end
v
%Or, using Jan's approach
v = {'12:00';'12:05';'13:45';'09:15';'08:25';'08:32';'15:20';'15:26'};
v=regexprep(v,'(?<=\d+\:\d)[1-4]','0')
v=regexprep(v,'(?<=\d+\:\d)[6-9]','5')
  2 Comments
Marc Jakobi
Marc Jakobi on 23 Sep 2013
Thanks to both for the awesome help! I decided to go for your second answer. All of them seem to work, but your second one and Jan's second approach are the fastest options.
Azzi Abdelmalek
Azzi Abdelmalek on 23 Sep 2013
Edited: Azzi Abdelmalek on 23 Sep 2013
regexp is slow. Both Jan's answers are faster then mine

Sign in to comment.

More Answers (1)

Jan
Jan on 23 Sep 2013
Edited: Jan on 23 Sep 2013
v = {'12:00';'12:05';'13:45';'09:15';'08:25';'08:32';'15:20';'15:26'};
for k = 1:numel(v)
w = v{k};
if w(5) < '5'
w(5) = '0';
else
w(5) = '5';
end
v{k} = w;
end
[EDITED] Less readable:
for k = 1:numel(v)
w = v{k};
w(5) = '0' + 5 * (w(5) >= '5'); % Implicit conversion to CHAR
v{k} = w;
end
  2 Comments
Marc Jakobi
Marc Jakobi on 23 Sep 2013
Thanks to both for the awesome help! I decided to go for Azzi's second answer. All of them seem to work, but your second approach and Azzi's second one are the fastest options.
Jan
Jan on 23 Sep 2013
I've suggested this method for another reason that the run time: It is simple and easy to debug. If a reader needs 5 seconds less to understand it, it does not matter, if it runs in 0.005 or 0.002 seconds.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!