Terminate while loop when user hits enter

26 次查看(过去 30 天)
Hi eveyone,
This is my while loop. It must end when user hits enter without entering a value . The loop allows for upto 3 successive blank entries before terminating. And, I cant get the n_points matrix to save entries without error.
How do I fix these?
Thank you for your time.
clear all
disp('Begin entering the data points')
t = 1;
x_point = input('enter x : ')
y_point = input('enter y : ')
n_points = [x_point,y_point];
while x_point ~= isempty(x_point) | y_point ~= isempty(y_point) % does not terminate immedeately when user presses enter without entering a value.
x_point = input('enter x : ')
y_point = input('enter y : ')
t = t+1
n_points(t,:) = [x_point,y_point];
end

采纳的回答

Jan
Jan 2021-4-11
编辑:Jan 2021-4-11
Avoid repeating code:
disp('Begin entering the data points')
t = 0;
while true % Infinite loop
x_point = input('enter x : ');
if isempty(x_point)
break; % Stop the WHILE loop
end
y_point = input('enter y : ');
if isempty(y_point)
break; % Stop the WHILE loop
end
t = t + 1;
n_points(t, :) = [x_point, y_point];
end
Or:
disp('Begin entering the data points')
t = 0;
ready = false;
while ~ready
x_point = input('enter x : ');
if ~isempty(x_point)
y_point = input('enter y : ');
end
if ~isempty(x_point) && ~isempty(y_point)
t = t + 1;
n_points(t, :) = [x_point, y_point];
else
ready = true;
end
end
  1 个评论
klb
klb 2021-4-12
Thanks Jan, this works. Much aprpeciate the two different while loop logics. With these, you have clarified the following as well.
For some reason I have been under the impresison that the loop terminating logic must only be provided at the start of loop, and statements in the loop run till that logic returns a false. I see now that a while loop can be terminated from inside the loop as well. Thank you!

请先登录,再进行评论。

更多回答(1 个)

Jonathan Clayton
Jonathan Clayton 2021-4-10
the code below looks at the number of elements that the user has inputed for that loop, and if that number is zero then the loop is broken.
if numel(x_point)+numel(y_point)==0
break
end
Add this code above n_points variable so that the empty vaibles are not added to your list.
  4 个评论
klb
klb 2021-4-11
编辑:klb 2021-4-11
Incorporated your code block as seen in code below. If you run it, skipping x is ok. But skipping y value copies the x value to y. If I add your code a second time with if numel(y_points)==0 edit, It works , but that is blunt.
A while loop shouldnt need further fixes. That is my original question - how to terminate the While loop at the very first time user hits enter without having adding data.
clear all
disp('Begin entering the data points')
t = 1;
x_point = input('enter x : ')
y_point = input('enter y : ')
n_points = [x_point,y_point];
while x_point ~= isempty(x_point) | y_point ~= isempty(y_point)
x_point = input('enter x : ')
y_point = input('enter y : ')
if numel(x_point)==0
break
end
t = t+1
n_points(t,:) = [x_point,y_point];
end
I tried another logic here but it also needs 2 of those code blocks. Cant understand why doesnt the loop break with the main while loop conditon itself. Any suggestions to eliminate in-loop intemedeate conditions checks?
clear all
t=0;
x_point = 0;
y_point = 0;
while numel(x_point)~= 0
x_point = input('enter x : ')
y_point = input('enter y : ')
if numel(x_point)==0
break
end
if numel(y_point)==0
break
end
t = t+1;
n_points(t,:) = [x_point,y_point]
end
n_points
klb
klb 2021-4-12
Jonathan, I am using the code block. I am also learning its ok to terminate a while loop from the inside the loop. Thanks for your time and help!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Multirate and Multistage Filters 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by