MATLAB Answers

problem with if condition

2 views (last 30 days)
milan pepa
milan pepa on 9 Mar 2012
hi, i have this problem with matlab. This is my m-file, but it doesnt work.
clear all
t=0:0.1:1;
k=length(t);
a=0;
for i=1:k
if a==0.8
disp('hi')
end
a=a+0.1
end
If i use this one
t=0:1:10;
k=length(t);
a=0;
for i=1:k
if a==8
disp('hi')
end
a=a+1
end
this one works well. The difference between first and second case is only that some variables are increased 10times. Please help. thank you

Accepted Answer

Aldin
Aldin on 9 Mar 2012
Solution:
Here, try this algorithm:
clear all
t=0:0.1:1;
k=length(t);
a=0;
for i=1:k
b = num2str(a);
if strcmp(b,'0.8');
disp('hi')
end
a=a+0.1;
end
  5 Comments
milan pepa
milan pepa on 9 Mar 2012
thank you Walter, it works great

Sign in to comment.

More Answers (1)

Community Treasure Hunt

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

Start Hunting!