From: E Thomson on 7 Jan 2010 10:01 I want to have a UI button up that just says something like 'STOP' and when they press it it causes the program to exit a for loop. For a silly example: ================== n=0; for i=1:100 if stop_pressed break end %if n=n+1; end %for loop disp(num2str(n)); ====================== Any ideas?
From: Matt Fig on 7 Jan 2010 20:45 A quick and dirty example, all in one M-File: function [] = stoploopgui() S.fh = figure('menubar','none',... 'units','pix',... 'pos',[400 400 100 50]); S.pb = uicontrol('string','stop',... 'callback',{@pb_call},... 'units','pixels',... 'position',[10 10 80 30]); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% S.pb(2) = uicontrol('visible','off'); n = 1; for ii = 1:inf if ~ishandle(S.pb(2)) break; end drawnow; n = n + 1; end disp(n) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% function [] = pb_call(varargin) % Callback for pushbutton delete(S.pb(2)) end end Note that if your loop is *fast* you could check ishandle and drawnow only every 200th or so iteration using mod. This would allow way more iterations of the loop for the same time elapsed.
From: ImageAnalyst on 7 Jan 2010 21:15 What I do is have a checkbox on my GUI that says "Finish now" and at the end of my loop I check its value. If the user checked it, I break out of the loop. The user is still able to check it despite them entering a loop by, say, clicking a pushbutton. Then when you exit the function, you clear the checkbox of course. You need to make sure the checkbox is cleared (unchecked) before you begin the loop of course. Sometimes I'll hide the checkbox until the loop begins, when I show it. Then I hide it again when I leave the function with the loop, so that it's only visible to the user while the loop is going.
From: E Thomson on 7 Jan 2010 16:02 Mike: many thanks that looks perfect I'll hack away at this from work tomorrow!
From: E Thomson on 8 Jan 2010 03:43 Again, thanks. I include here a copy of your function, with some comments to explain it for anyone who might be curious (and for myself). In particular, I'm not sure what the 'drawnow' line does. I looked up the doc, and don't quite get it. I added a couple of things (e.g., now when you push the button it is closed, there is a slight pause in each loop, the function returns n, and displays n each loop). ============= function n = stoploopgui2() %Figure/handle S.fh = figure('menubar','none',... 'units','pix',... 'pos',[400 400 100 50]); %Push button/handle S.pb = uicontrol('string','stop',... 'callback',{@pb_call},... 'units','pixels',... 'position',[10 10 80 30]); %Creates arbitrary invisible ui/handle to check/delete S.pb(2) = uicontrol('visible','off'); %MAIN LOOP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% n = 1; for ii = 1:9000 if ~ishandle(S.pb(2)) break; end %if ~ishandle drawnow; %not sure what this does pause(.01) disp(n) n = n + 1; end %for loop disp(['Ended after ' num2str(n) ' loops.']) %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %Subfunctions function [] = pb_call(varargin) % Callback for pushbutton delete(S.pb(2)) %removes the invisible GUI close hidden %Closes open button windows end %subfunction pb_call end %function stoploopgui
|
Next
|
Last
Pages: 1 2 Prev: read input variables from a file Next: C# To call a Compiled DLL:Error |