Problems with my loop
2 次查看(过去 30 天)
显示 更早的评论
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
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/
回答(1 个)
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
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!