Problems with my loop

1 次查看(过去 30 天)
flashpode
flashpode 2021-9-21
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 个评论
Stephen23
Stephen23 2021-9-21
"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/
flashpode
flashpode 2021-9-21
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).

请先登录,再进行评论。

回答(1 个)

Mathieu NOE
Mathieu NOE 2021-9-21
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

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by