Applying a string of user inputs within a while-loop

Hi folks, realy new to MATLABS and with the recent events forcing campus closure I am having some difficulties without the in class help from the instructor.
I am attempting to code a simple calculator for projectile motion and have come up with thus so far:
How can I use the while loop to request whether or not to continue with the calculation? Really am not sure what I am doing incorrectly here.
This is the error I am presented with: Line 11, Collumn 5: "At least one END is missing: the statement may begin here."
Thank you for any help!
angle=input(['Enter the angle of trajectory [degrees] : '])
height=input(['Enter the height at which the projectile will be launched [feet] : '])
velocity=input(['Enter the velocity as the projectile is initially launched [feet/second] : '])
disp(['You have selected: ANGLE = ' num2str(angle,'%5.3f') ' degrees; HEIGHT = ' num2str(height,'%10.3f') ' feet; VELOCITY= ' num2str(velocity,'%10.3f') ' ft/s'])
while resume=input(['Would you like to continue with these calculations?','s'])
quit='NO'
cont='YES'
if ischar(resume) && strcmp(resume,quit)
disp(['Restarting caclulations...'])
break
else if ischar(resume) && strcmp(resume,cont)
disp(['Solving for projectile motion...'])
continue
end

 Accepted Answer

michael - for the error that you mention ("At least one END is missing: the statement may begin here"), this can be fixed by adding the missing end to the (in this case) while loop. Another error has to do with the while condition
while resume=input(['Would you like to continue with these calculations?','s'])
. An assignment cannot be made here as this statement does not evaluate to a boolean (true/false) and so doesn't quite make sense. You may want to do something like
while true
resume=input('Would you like to continue with these calculations?','s');
quit='NO';
cont='YES';
if ischar(resume) && strcmpi(resume,quit)
disp('Restarting caclulations...')
break;
elseif ischar(resume) && strcmpi(resume,cont)
disp('Solving for projectile motion...')
continue
end
end
Note some of the changes when compared against your code: use of strcmpi for case insensitive comparisons, removal of [] around strings since we aren't doing any concatenations, and use/placement of 's' in the input command. While the above works, it may not be exactly what you want. Why do you want the while loop? Is it so that the user can enter in valid angle, height, and velocity of the projectile? If they do not want to continue with these calculations, do you want to give them the opportunity to enter in new ones? Would you break out of the loop and solve the projectile motion only if the user wishes to continue with these calculations? Or do you want to continue solving for the projectile motion for various sets of inputs until the user wants to quit?

3 Comments

Hi Geoff, thank you for the very informative answer.
I am creating the while loop in order to restart the code in the event the user inputs incorrect values and I wanted to the option to restart the code if so from the get go. The requested inputs of height/angle/velocity will then be placed within an anonymous function and used in a for-loop to calculate various set(s) of inputs until user requests to quit the code.
Eventually I plan to replace the yes/no options with 1 and 0 so I can easily use the code with a ti-84 caclulator.
I have been messing with it for the past few hours and changed the math to something simpler with one less user input.
while true
pipe_dia=input('Enter the diameters of the pipe(s) [feet]: ')
velocity=input('Enter the velocity as the of the water [feet/second] : ')
disp(['You have selected: DIAMETER = ' num2str(pipe_dia,'%5.3f') '; VELOCITY= ' num2str(velocity,'%10.3f') ' ft/s'])
resume=input('Would you like to continue with these calculations?','s');
quit='NO';
cont='YES';
if ischar(resume) && strcmpi(resume,quit)
disp('Restarting caclulations...')
break;
elseif ischar(resume) && strcmpi(resume,cont)
disp('Solving for volumetric flow rate...')
disp([num2str(FlowRate,'%10.5f'),' [ft^3/s]'])
continue
end
end
Flowrate_Circular_Pipe=@(x,y) y.*(pi.*((x/2)/12).^2)
FlowRate=Flowrate_Circular_Pipe(pipe_dia,velocity)
I have made much progress with your help - the yes and no function are no longer giving me errors and I am able to calculate the flowrate.
I am confused however; when I input 'no' the code still calculates the flowrate but I would like to instead start the code from the beginning. Other than that I need to figure out how to loop a 'yes' answer back to user inputs in the beginning - currently it keeps displaying the string 'Would you like to continue with these calculations?'
Rather than using break could I perhaps implement a different code to loop back to top?
michael - in your code, when the user enters "no", then you call
if ischar(resume) && strcmpi(resume,quit)
disp('Restarting caclulations...')
break;
Calling break will exit the while loop. If you want to "loop back to top" then just remove this line (you could replace this with continue but since there is no code outside of this if/elseif block, then it doesn't really matter). As for when the user enters "yes", don't you want to exit the while loop? Or shouod these two lines
Flowrate_Circular_Pipe=@(x,y) y.*(pi.*((x/2)/12).^2)
FlowRate=Flowrate_Circular_Pipe(pipe_dia,velocity)
be inside the elseif block like
while true
pipe_dia=input('Enter the diameters of the pipe(s) [feet]: ')
velocity=input('Enter the velocity as the of the water [feet/second] : ')
disp(['You have selected: DIAMETER = ' num2str(pipe_dia,'%5.3f') '; VELOCITY= ' num2str(velocity,'%10.3f') ' ft/s'])
resume=input('Would you like to continue with these calculations?','s');
quit='NO';
cont='YES';
if ischar(resume) && strcmpi(resume,quit)
disp('Restarting caclulations...');
break;
elseif ischar(resume) && strcmpi(resume,cont)
disp('Solving for volumetric flow rate...');
Flowrate_Circular_Pipe=@(x,y) y.*(pi.*((x/2)/12).^2);
FlowRate=Flowrate_Circular_Pipe(pipe_dia,velocity);
disp([num2str(FlowRate,'%10.5f'),' [ft^3/s]']);
continue; % <----- break out of the loop or start again?
end
end
Geoff,
Thank you so much! I see I certainly lacked quite an important understanding of the break/continue commands but your explanation along with David's help has really put that into perspective.
I do want to continue the loop and after messing around with the code a bit I see where my mistakes were. For future reference this is what I ended up with. I also had to change the forumla a bit as it was solving for inches and not feet.
while true
pipe_dia=input('Enter the diameter of the pipe [feet]: ')
velocity=input('Enter the velocity of the of the water [feet/second] : ')
disp(['You have selected: DIAMETER = ' num2str(pipe_dia,'%5.3f') '; VELOCITY= ' num2str(velocity,'%10.3f') ' ft/s'])
resume=input('Would you like to continue with these selected values?','s');
quit='NO';
cont='YES';
if ischar(resume) && strcmpi(resume,quit)
disp('Restarting caclulations...');
clear all
close all
elseif ischar(resume) && strcmpi(resume,cont)
disp('Solving for volumetric flow rate...');
Flowrate_Circular_Pipe=@(x,y) y.*(pi.*(x/2).^2);
FlowRate=Flowrate_Circular_Pipe(pipe_dia,velocity);
disp([num2str(FlowRate,'%10.5f'),' [ft^3/s]']);
disp('...restarting program for new calculations...')
continue
end
end
Thank you again, you really helped me out tons!

Sign in to comment.

More Answers (1)

Recommend breaking while loop with if statement:
while 1
angle=input(['Enter the angle of trajectory [degrees] : ']);
height=input(['Enter the height at which the projectile will be launched [feet] : ']);
velocity=input(['Enter the velocity as the projectile is initially launched [feet/second] : ']);
disp(['You have selected: ANGLE = ' num2str(angle,'%5.3f') ' degrees; HEIGHT = ' num2str(height,'%10.3f') ' feet; VELOCITY= ' num2str(velocity,'%10.3f') ' ft/s']);
%put calculation here
resume=input(['Would you like to continue with these calculations?','s']);
if isequal(lower(resume),'no')||isequal(lower(resume),'n')
break;
end
end

1 Comment

Hi David,
Thank you for the information! Placing those inputs within the loop was a key to helping me figure this out. I figured out a code which I have included in a response to Geoff future reference. The help is much appreciated!

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 1 Apr 2020

Edited:

on 2 Apr 2020

Community Treasure Hunt

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

Start Hunting!