Running a for loop with breaks

5 views (last 30 days)
Reed Harper
Reed Harper on 18 Jan 2021
Answered: Cris LaPierre on 18 Jan 2021
My goal is to get coordinates using ginput, and then asking the user if there is another set of coordinates to pick. if yes continuing looping and get another set of coord. from ginput, if no, then I'd like to break out of the loop. When I use the code below no matter my input to the yes/no, it already moves to selecting new points. Thoughts?
for n=1:12
[temp(:,1),temp(:,2)]=ginputc('Color','r','ShowPoints',true);
platelets{k}{p}=[temp(:,1),temp(:,2)];
p=p+1;
plateCheck=input('\nIs there an untagged platelet in frame, 1 for yes 0 for no.\n');
if plateCheck == 0
break;
else
end
end
  1 Comment
Bob Thompson
Bob Thompson on 18 Jan 2021
I had to remove the ginputc portion of this code, but doing so made it run fine for me.
Are you sure you're not overwriting the plateCheck value within ginputc somehow? I can't imagine this being the case, but it's the only thing that doesn't seem to add up to me.

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 18 Jan 2021
First point - you are not using ginput. You are using ginputc, which is a function obtained on the file exchange.
From the description provided there, you will see that ginputc will continue collecting points until you press the return key.
  • [X,Y] = ginputc gathers an unlimited number of points until the return key is pressed.
I suspect the return key must be pressed when the figure is in focus. The input command is switching the focus to the command window. Try specifying how many points you want the user to select in each loop to avoid this behavior.
  • [X,Y] = ginputc(N) gets N points from the current axes and returns the X- and Y-coordinates in length N vectors X and Y.

Categories

Find more on Visual Exploration 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!