Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

How to stop a for loop execution from GUI?(From Toolbar stop button)

Asked by Arun Badigannavar on 4 Apr 2013

I have for loop,,I want to to stop this loop from a toolbar button,, Initially I have set a flag "STOP=0",,,and when I press my stop button in tool bar,,I am calling a function,,where "STOP=1" I have written script,,,,But my script is not considering this"STOP" flag,,,please hep me out>...

0 Comments

Arun Badigannavar

Tags

Products

2 Answers

Answer by ChristianW on 4 Apr 2013
Accepted answer

You can write your flag in the UserData-property of the toolbaricon.

...
set(handles.uitoolbar1,'UserData',0)
for ...
  if get(handles.uitoolbar1,'UserData')
      break
  end
end
...
function uipushtool1_ClickedCallback(hObject, eventdata, handles)
set(handles.uitoolbar1,'UserData',1)

3 Comments

Arun Badigannavar on 5 Apr 2013

for loop is executing in other function than main figure...

Arun Badigannavar on 5 Apr 2013
set(handles.uitoolbar1,'UserData',0)
for i=1:1:inf
   if get(handles.uitoolbar1,'UserData')
    break
   end
      a=i
      pause(0.00000000000000000000001)
  end

If I put pause ,,then only its working,,,

Jan Simon on 5 Apr 2013

The tiny interval for PAUSE is not useful. Use pause to get the shortest possible delay. Usually drawnow is used to allow ui-objects to update.

ChristianW
Answer by Jan Simon on 4 Apr 2013

Each function has its own workspace, such that a variable set in one function is not visible in the other. A solution is to set a flag e.g. in the UserData of a figure or any other UI-object. This flag can be checked from inside the loop also.

0 Comments

Jan Simon

Contact us