Problems with my loop
조회 수: 1 (최근 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에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!