# Problems with my loop

1 view (last 30 days)
vicente Noguer on 21 Sep 2021
Answered: Mathieu NOE on 21 Sep 2021
Hey I made a loop that compares a string I got with a number.
P = size(t1,1);
h = 0;
for s = 1:1:P
if strcmp(t1(s), '5959') %compares if in the string (t1) there are those numbers, if so
% if the next line(Number) if it is 0000 add
% h to t1
if strcmp(t1(s+1), '0000')
h= h+1;
if h == 24 % if h get to 24 then it goes another time to 0
h = 0;
end
T_Hour1 = num2str(h) + t1;
end
end
end
I think I've got many errors but the one first one that I got is:
if strcmp(t1(s+1), '0000')
could anybody help me to make this work? and if you see a better solution to get to the solution share it.
Thank you!!!!
##### 2 CommentsShowHide 1 older comment
vicente Noguer on 21 Sep 2021
Those are the problems:
Index exceeds the number of array elements (403432).
Error in CodigoBo (line 13)
if strcmp(t1(s+1), '0000')
and as you said is a string of numbers that go from 0000 to 5959(minutes and secods). every time reaching 5959(there are many concadenated) it goes another time to 0000. What I want is each time it goes to 0000 put a number in front of it that goes from the 0 to 23 (hours).

Mathieu NOE on 21 Sep 2021
hi
I tried to guess what the intention was, and came to this suggestion, although I have no idea what the output should be
maybe if you had supplied an inout data and some indications of what the output should be, I could be more effective.
all the best
t1 = [" balljkvjkjvk ";...
" x 5959 ";...
" bal 0000 ";
" balljkvjkjvk ";...
" f 5959 ";...
" 0000 frg "];
P = size(t1,1);
h = 0;
for s = 1:1:P
if contains(t1(s), '5959') %compares if in the string (t1) there are those numbers, if so
% if the next line(Number) if it is 0000 add
% h to t1
toggle = 1; % set toggle
end
if (toggle == 1 && contains(t1(s), '0000'))
h= h+1;
if h == 24 % if h get to 24 then it goes another time to 0
h = 0;
end
T_Hour1 = num2str(h) + t1;
toggle = 0; % reset toggle
end
end