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
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
Ajay 2012년 10월 8일
Undefined function or variable 'K'.
Error in InteriorAngle (line 2) switch in{K}
Error in Lab05 (line 12) out = InteriorAngle(in)
^Those are the errors I'm getting, so I don't get an error on the recursive limit being reached?
Daniel Shub
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
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
Ajay 2012년 10월 8일
Okay, so now with the above edit, i'm getting that the Maximum recursion limit of 500 reached. why is this happening?
Daniel Shub
Daniel Shub 2012년 10월 8일
Look back at my first comment...
Walter Roberson
Walter Roberson 2012년 10월 8일
Question content has been edited out of existence by the original poster :(
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?

댓글을 달려면 로그인하십시오.

 채택된 답변

Matt Fig
Matt Fig 2012년 10월 8일
편집: Matt Fig 2012년 10월 8일

1 개 추천

You are recursively calling the function from inside the function with no way to terminate the recursion. Additionally, you have named a variable the same name as a MATLAB function (INPUT). In general, you should avoid this practice as it masks the function.
Once you take care of these problems, put your switch inside a FOR loop that loops over the length of the input argument. You switch on IN{ii}.

댓글 수: 4

Ajay
Ajay 2012년 10월 8일
i tried doing that, but i'm getting "Index exceeds matrix dimensions"
Matt Fig
Matt Fig 2012년 10월 8일
Show your new code in a comment below, please.
function output = InteriorAngle(input)
for s=1:8,
switch input ('s')
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
end
Why is s always 1-through-8? What if the length of input is only 3 as in your example?
for ii = 1:length(IN)
switch IN{ii}

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ajay
Ajay 2012년 10월 8일

0 개 추천

it worked and i get it, thanks!

카테고리

도움말 센터File Exchange에서 Performance and Memory에 대해 자세히 알아보기

질문:

2012년 10월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by