one case can not be initiatated when plotting using switch-case

when running the following code, case n=0.6 can not be implemented.
for n=0.2:0.2:0.8
switch n
case 0.2
h1=plot(1:3,sin(1:3)+n,'-.xr');
case 0.4
h2=plot(1:3,sin(1:3)+n,'-.xg');
case 0.6
h3=plot(1:3,sin(1:3)+n,'-.xk');
case 0.8
h4=plot(1:3,sin(1:3)+n,'-.xb');
end
hold on;
end
however, when I change the range of n from 0.2:0:2:0.8 to 1:4, everthing is Ok! I dont know why ,if anyone can help explain, thanks a lot!
for n=1:4
switch n
case 1
h1=plot(1:3,sin(1:3)+n);
case 2
h2=plot(1:3,sin(1:3)+n);
case 3
h3=plot(1:3,sin(1:3)+n);
case 4
h4=plot(1:3,sin(1:3)+n);
end
hold on;
end
legend('1','2','3','4')

 採用された回答

per isakson
per isakson 2012 年 5 月 19 日

0 投票

!!! floating point arithmetic suffers from rounding errors !!!
Try: format hex
On my system your example works, but
for 0.0 : 0.3 : 1
does not

3 件のコメント

Lin LI
Lin LI 2012 年 5 月 19 日
thanks a lot, isakson! I modified the code to the following , but it still doesnt work.
for n=0.2:0.2:0.8
aa=fi(n,1,8,3);
tt=hex(aa);
switch tt
case 02
h1=plot(1:3,sin(1:3)+n,'-.xr');
case 03
h2=plot(1:3,sin(1:3)+n,'-.xg');
case 05
h3=plot(1:3,sin(1:3)+n,'-.xk');
case 06
h4=plot(1:3,sin(1:3)+n,'-.xb');
end
hold on;
kkk=3;
end
legend('1','2','3','4')
Lin LI
Lin LI 2012 年 5 月 19 日
I add ' ' to 02, and now it works.
Lin LI
Lin LI 2012 年 5 月 19 日
Is that the reason?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by