While loop not breaking even though condition is met.

51 views (last 30 days)
I have an issue where my while loop continues to run even though my if statement, containing a break, inside the loop is met. I have tested this by displaying strings. It does print 'moving stage' when a push the button as well as 'stop stage' when i push it again. The flags I have defined changes between 1 or 0 depending on a button i push in a GUI. How can I terminate my loop?
function Standa_cmd(Stage_functions_settings)
while 1
pause(1)
disp('moving stage')
flag = Stage_functions_settings.move_x; % This struct field changes value between 1 or 0 depending on the button push in the gui
if(flag == 0)
disp('stop stage')
break
end
end
end
  3 Comments
Jiri Hajek
Jiri Hajek on 11 Nov 2022
Hi, what you have described does not prove that the break command does not work, which you seem to suggest. Please put a breakpoint inside the if block and then use the F10 key to see what happens... This way, if you are able to get the process to the breakpoint, you will verify the correct functioning of the break command.
Christoffer Sørensen
Christoffer Sørensen on 11 Nov 2022
@VBBV I checked the class of the "Stage_functions_settings.move_x" field and it is a double, so I'm not sure that is the problem. In the app, I just use the value of the button to set the field of the struct to either 0 or 1. So the value of the button is not passed on to any other function.

Sign in to comment.

Accepted Answer

Bruno Luong
Bruno Luong on 11 Nov 2022
Edited: Bruno Luong on 11 Nov 2022
Just guessing; Might be it breaks but your function is entered again or there are many instances of function in queue, log more
function Standa_cmd(Stage_functions_settings)
disp('Standa_cmd is called')
while 1
pause(1)
disp('moving stage')
flag = Stage_functions_settings.move_x; % This struct field changes value between 1 or 0 depending on the button push in the gui
if(flag == 0)
disp('stop stage')
break
end
end
disp('I''m out of the loop and about to quit the function')
end
  13 Comments
Bruno Luong
Bruno Luong on 17 Nov 2022
Edited: Bruno Luong on 17 Nov 2022
General principe : finite state machine
I wouldn't rely at all on push button state to make the state mahine (stage run or stop). I recommed this workflow:
I would create the state machine at the single place (a function to handle it) using for example persistent variable.
A second persistent logical variable called StopRequested.
Inside the while loop of the function calls drawnow then check StopRequested. If it's TRUE breaks the while loop.
Everytime user clicks on the button, the callback does this:
  • If (and only if) the current state is stop it invokes Standa_cmd
At the begining of function Standa_cmd, it switch StopRequested to FALSE and toggles the state to run (in this order)
The function setups an onCleanup callback that is triggered only when the function quits. The onCleanup callback function toggles the state to stop
The function runs the while loop
  • Otherwise (the current state is run) switch StopRequested to TRUE (to signal the while loop to break), do nothing else.
Christoffer Sørensen
Christoffer Sørensen on 21 Nov 2022
Thank you for the elaborate answer! I will try to impliment this. In the mean time I managed to make a while loop terminate. But I will try to implement a more robust scheme for the future. Below is my temporary solution:
function StateButtonValueChanged(app, event)
flag = 1;
while(flag)
f = app.StateButton.Value;
app.Stage_functions_settings = setfield(app.Stage_functions_settings,'move_x',1);
Equipment_calls(app.Equipment_list,app.Equipment_libs,app.Stage_functions_settings)
drawnow;
if isequal(f,0)
app.Stage_functions_settings = setfield(app.Stage_functions_settings,'move_x',0);
Equipment_calls(app.Equipment_list,app.Equipment_libs,app.Stage_functions_settings)
flag = 0;
break;
end
end
end

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 11 Nov 2022
I think the pushbutton does not set the flag. I've seen this in GUIDE and this is why I use checkboxes instead of a push button. Then simply check the value of the checkbox inside your while loop.
Nonetheless it is possible to use a push button. See attached little demo.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!