How to exit a for loop when only return has been done with no input data?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
How can I get a for loop to break when the next input entry is empty? ie you only press return instead of entering an another point
for i = 1:99999
point(i,:)=input('Enter a point [x y]: ')
if
end
points=point;
end
Accepted Answer
madhan ravi
on 2 Dec 2018
Edited: madhan ravi
on 2 Dec 2018
point(i,:)=input('Enter a point [x y]: ','s')
% ^--^----include this
if isempty(point(i,:))==1
break
end
10 Comments
Niklas
on 2 Dec 2018
The complete function code,
Got following error
Enter the number of points (exit with return)
Enter a point [x y]: > [2 2]
point = [2 2]
Enter a point [x y]: >
error: askpoints: =: nonconformant arguments (op1 is 1x5, op2 is 0x0)
function points=askpoints()
disp('Enter the number of points (exit with return)')
for i = 1:9999
point(i,:)=input('Enter a point [x y]: ','s')
if isempty(point(i,:))==1
break
end
end
points=point
end
madhan ravi
on 2 Dec 2018
instead of > just press enter to have an empty set
Niklas
on 2 Dec 2018
sorry, I don't realy follow how I should do toavoid the error
madhan ravi
on 2 Dec 2018
Edited: madhan ravi
on 2 Dec 2018
function points=askpoints()
disp('Enter the number of points (exit with return)')
point=cell(1,9999);
for i = 1:9999
point{i}=input('Enter a point [x y]: ','s') % just press enter to stop the loop
if isempty(point{i})==1
return
end
end
points=[point{:}]
end
Niklas
on 2 Dec 2018
is loops
Enter the number of points (exit with return)
Enter a point [x y]: > [2 2]
point =
{
[1,1] = [2 2]
[1,2] = [](0x0)
[1,3] = [](0x0)
[1,4] = [](0x0)
[1,5] = [](0x0)
[1,6] = [](0x0)
[1,7] = [](0x0)
madhan ravi
on 2 Dec 2018
when there is no input you want to exit the loop that was your question right?
Niklas
on 2 Dec 2018
exact,
example:
[1 1] return
[2 2] return
return (the for loop should be exit)
madhan ravi
on 2 Dec 2018
That‘s what happens when you try my latter comment
error: askpoints: =: nonconformant arguments (op1 is 1x5, op2 is 0x0)
That is not an error message generated by MATLAB. It appears that it might be an Octave error message. This is not an Octave support forum.
madhan ravi
on 2 Dec 2018
sir Walter really your a genius , not ina million years I would have known that if you would have not commented that it belongs to octave ?
More Answers (0)
Categories
Find more on Time Series Events in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)