MATLAB: Toggle button to stop a for loop not working

matlab gui toggle button start/stop

I am trying to exit a for loop using toggle button, created using guide. Toggle button is initially called 'Run'. The iteration starts when 'Run' is hit and the toggle button string changes to 'Stop'. However, iteration continues after 'Stop' is hit (in the screenshot, 'Stop' is hit at i=2). I used dbstack to check the Run callbacks. Am I missing something obvious?? Any help would be appreciated. Here's the code snippet and the command window screenshot-

Best Answer

I don't have time to debug your code tonight, but you can try my demo of basically the same thing. attached.