Problems with my loop
1 view (last 30 days)
Show older comments
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 Comments
Stephen23
on 21 Sep 2021
"could anybody help me to make this work?"
Please show us the complete error message. This means all of the red text.
"... if you see a better solution to get to the solution share it."
It looks like you are working with dates/times, in which case manipulating strings like that is not likely to be a good approach.
Please explain your actual task: https://xyproblem.info/
Answers (1)
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
0 Comments
See Also
Categories
Find more on Characters and Strings 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!