MATLAB: How to stop while statement

while loop

function isitalike=comparestrings(a,b)
n=length(a);
m=length(b);
c=logical(0);
y=m;
z=n;
f=0;
if m>n
temp=a;
a=b;
b=temp;
end
for i=1:n
c=strcmp(a(i),b(1))
if c
j=1;
if a(i:n)>=length(b)
for k=1:length(b)
while a(i)==b(j)
if j==length(b)
if z>y
disp('b is in a')
f=1;
else
disp('a is in b')
f=1;
end
end
j=j+1;
i=i+1;
end
end
end
end
end
if f==0
if z>y
disp('b is not in a')
else
disp('a is not in b')
end
end
The function above does not work properly I can't stop the while statement after i have found that a is in b or b is in a. This is what happens:
>> a
a =
agg
>> b
b =
sagggsa
>> comparestrings(a,b)
c =
0
c =
1
a is in b
??? Attempted to access b(4); index out of bounds because
numel(b)=3.
Error in ==> comparestrings at 39
while a(i)==b(j)
>>

Best Answer

What is your statement
if a(i:n)>=length(b)
intended to mean?
Your while loop continues indefinitely because you do not have an condition defined to escape from it with a "break" statement.
Related Question