How to Get a script to end if a certain input is entered
17 views (last 30 days)
Show older comments
Ashlee Rogers
on 26 Oct 2020
Commented: Walter Roberson
on 26 Oct 2020
I am new to MATLAB, and I have a general question about getting a script to end when an input is entered that does not fulfill the requirements. I know that it requires some sort of "for" statement, but i'm not sure how to inplement this.
I am currently working on a problem that I can use as an example:
Create a script that asks the user repeatedly for two time intervals in the form of hours, minutes, and seconds, and provides the addition of these two time intervals in the same form. The script should end if the input from the user is zero for all six values. You should write a function which takes in the given time intervals and returns their addition. The script will handle the user input, call the function, and display the corresponding output.
This is what I have so far:
clc
clear
flag = true;
while flag
disp('Do you want to enter a time interval?')
answer = input('Enter 1 for YES and 0 for NO :')
if answer == 1
hms1 = input("Enter the first time interval using 6 digits representing hours, minutes, and seconds: ");
hms2 = input("Enter the second time interval using 6 digits representing hours, minutes, and seconds: ");
intervalAddition(hms1, hms2);
disp(hms1);
disp(hms2);
else
flag = false;
end
end
How do i get the script to end if the input is zero for all six values?
0 Comments
Accepted Answer
Walter Roberson
on 26 Oct 2020
if hms1 == 0 || hms2 == 0
flag = false;
else
intervalAddition(hms1, hms2);
disp(hms1);
disp(hms2);
end
2 Comments
Walter Roberson
on 26 Oct 2020
No. That is applicable to the specific situation where you already have a termination flag set up.
The solution that is applicable to any situation is to set a termination flag and use break , and when you are within a nested loop, that the nesting loop should immediately test the termination flag and break if set -- and that should keep happening for every level of nested loop.
To clarify: break leaves only the inner-most level of nested loop. There are some computer languages where their break operation accepts an optional count of how many levels to exit, but MATLAB does not support that.
all_hms = zeros(1,10);
for K = 1 : 10
still_going = true;
while true
hms1 = input( sprintf('Enter time interval #%d using 6 digits representing hours, minutes, and seconds: ', K));
if hms1 == 0
still_going = false; %user wants to terminate outer loop
break;
end
if ~isnumeric(x) || ~isscalar(x) || x < 0 | x >= 1e7
disp('Enter a 6 digit positive integer');
else
break; %we got what we need for the inner loop but we want to continue outer loop
end
end
if ~still_going; break; end
all_hms(K) = hms1;
end
Notice the termination flag is not set to false for the case of valid input being recognized, but that if the user enters invalid input then the code loops back to ask again within the same K value -- K is controlling number of full cycles of getting valid input, not the number of times we ask the user for input. This code is not "10 chances and you are out!" -- this code is "10 times, make as many mistakes as you want to enter a valid entry, but give up everything if 0 is entered."
More Answers (0)
See Also
Categories
Find more on Dates and Time in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!