switch case construction help
古いコメントを表示
(Ajay deleted the question so I (MF) am restoring it as best I can.)
I wrote this body of code:
function output = InteriorAngle(input)
switch input (InteriorAngle)
case 'triangle'
disp ('180')
case 'square'
disp ('360');
case 'pentagon'
disp ('540');
case 'hexagon'
disp ('720');
case 'heptagon'
disp ('900');
case 'octagon'
disp ('1080');
case 'nonagon'
disp ('1260');
case 'decagon'
disp ('1440');
otherwise
disp ('0');
end
end
but for some reason, it's not running when i input,
in = cell(1,3);
in{1,1} = 'triangle';
in{1,2} = 'hexagon';
in{1,3} = 'dodecagon';
out = InteriorAngle(in)
what am i doing wrong?
8 件のコメント
Daniel Shub
2012 年 10 月 8 日
Not running is not a helpful description. What error do you get? When I run it I get an error about recursive limit being reached. Do you understand why your function is recursively calling itself?
Ajay
2012 年 10 月 8 日
Daniel Shub
2012 年 10 月 8 日
Then you are not running the posted code since line 2 of the InteriorAngle function you posted is not "switch in{K}".
Daniel Shub
2012 年 10 月 8 日
Okay, I can reproduce the error with your new code. The error suggests that K is used on line 2 (which you can see it is), but that it is not defined before that. Since it is the 2nd line, it is pretty obvious that you have not defined K. You are also going to have a problem with "in" being undefined also. These errors are picked up by mlint, and you should pay attention to them.
Ajay
2012 年 10 月 8 日
Daniel Shub
2012 年 10 月 8 日
Look back at my first comment...
Walter Roberson
2012 年 10 月 8 日
Question content has been edited out of existence by the original poster :(
Matt Fig
2012 年 10 月 9 日
Saved from google cache:
I wrote this body of code:
function output = InteriorAngle(input)
switch input (InteriorAngle)
case 'triangle'
disp ('180')
case 'square'
disp ('360');
case 'pentagon'
disp ('540');
case 'hexagon'
disp ('720');
case 'heptagon'
disp ('900');
case 'octagon'
disp ('1080');
case 'nonagon'
disp ('1260');
case 'decagon'
disp ('1440');
otherwise
disp ('0');
end
end
but for some reason, it's not running when i input,
in = cell(1,3);
in{1,1} = 'triangle';
in{1,2} = 'hexagon';
in{1,3} = 'dodecagon';
out = InteriorAngle(in)
what am i doing wrong?
採用された回答
その他の回答 (1 件)
カテゴリ
ヘルプ センター および File Exchange で Performance and Memory についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!