How to exit a for loop when only return has been done with no input data?

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

point(i,:)=input('Enter a point [x y]: ','s')
% ^--^----include this
if isempty(point(i,:))==1
break
end

10 Comments

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
instead of > just press enter to have an empty set
sorry, I don't realy follow how I should do toavoid the error
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
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)
when there is no input you want to exit the loop that was your question right?
exact,
example:
[1 1] return
[2 2] return
return (the for loop should be exit)
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.
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 ?

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!