Terminate while loop when user hits enter
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
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
2021년 4월 11일
Jonathan Clayton
2021년 4월 11일
ok so that is a simple fix
if numel(x_point)==0
break
end
add this if statement directly after where the user adds the x_point in the while loop. this will break the loop if the user does not input anything when promted and simply hits enter.
klb
2021년 4월 12일
카테고리
도움말 센터 및 File Exchange에서 Clocks and Timers에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!