If-elseif-else-end structure doesn't work properly

조회 수: 7 (최근 30일)
lbon jbn
lbon jbn 2011년 11월 23일
Hello, I am having some issues with a volume calculating program I am trying to create. For whatever reason, after a volume is calculated, the program does not end, it jumps to the next solid to calculate its volume. For example, if I have just calculated the volume of a sphere, it will then go to the cube function and begin asking for inputs related to that function. I don't understand why it just doesn't end. When the functions are run separately, they end fine. But when ran through the 'main' program (the piece of code below), the issue I mentioned pops up. Any suggestions?
solid = input('Welcome to Volume Calculator - Please enter the solid for which you would like to calculate the volume: ');
if solid == cube
volume = cube(a)
elseif solid == sphere
volume = sphere(r)
elseif solid == prism
volume = prism(a,b,c)
else disp('Only certain solids can be calculated. Please try again')
end

채택된 답변

Walter Roberson
Walter Roberson 2011년 11월 23일
Consider your lines
if solid == cube
volume = cube(a)
The second of those lines shows us that "cube" is a function. In the "volume =" line, the function is invoked with one argument.
Now look again at the first of those lines. "cube" appears there, and we know that "cube" is a function, so when "cube" occurs there, that means to invoke the function "cube" with no arguments, just as if you had written
if solid == cube()
Then whatever is returned from the cube() function will be compared to the value you input for "solid". As those values probably do not match, the "elseif" branch will be invoked, but the code there has the same issue...
I predict that the corrected code would be
solid = input('Welcome to Volume Calculator - Please enter the solid for which you would like to calculate the volume: ', 's');
if strcmpi(solid,'cube')
volume = cube(a)
elseif strcmpi(solid, 'sphere')
volume = sphere(r)
elseif strcmpi(solid, 'prism')
volume = prism(a,b,c)
else disp('Only certain solids can be calculated. Please try again')
end

추가 답변 (1개)

lbon jbn
lbon jbn 2011년 11월 24일
What you guys said worked except for the fact that I had to remove the input arguments for the functions so rather than prism (a,b,c) it was just prism (it was giving an error when I had the input arguments in).
Thank you both for your help...I was also just wondering though, if I want the program give an error message when someone types in a negative value for one of the input arguments, how would that work? I have done it for a scalar but there is an option to enter a vector (so a range of values) - and I'm not sure how to make the if statement identify whether or not a single element out of the entire array is negative.This is what I have for a scalar and it works fine:
if r < 0
disp ('r cannot be a negative value. Please try again.')
else volume = (4/3)*pi.*r.^3 ;
end
  댓글 수: 2
Walter Roberson
Walter Roberson 2011년 11월 24일
if any(r < 0)
if all(r < 0)
lbon jbn
lbon jbn 2011년 11월 24일
awesome, thanks bro!

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

카테고리

Help CenterFile Exchange에서 Spline Postprocessing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by