开发者

MATLAB exiting loop in function when using GUI

开发者 https://www.devze.com 2023-02-19 21:08 出处:网络
I have a function which runs when a button is clicked and that function will call another function to perform plotting, which is an animation composed by a series of loops. Problem is, after the progr

I have a function which runs when a button is clicked and that function will call another function to perform plotting, which is an animation composed by a series of loops. Problem is, after the program is run, it cannot be stopped unless pressing Ctrl+C which then cause the whole .exe to fail and require reopening it.

I want to make a Reset button, so to stop the loop from running, but how to pass a new variable from the interrupt to the existing function which is two level above the stack?

Thank You

edit @ 22:13 27/3/2011 (UKT)

a more clearer of what I am trying to do:

function push_calculate_Callback(hObject, eventdata, handles)
    (List of parameters read in the GUI)
    fmain(list of p开发者_如何学Caramters required to pass to the function, fmain)

that's first part of the code which linked directly to the GUI, the fmain looks like:

function fmain(List of parameters)
(List of calculations...)
fplotting(list of paramters for plotting)

in which the fplotting is where the animation comes out from, inside the fplotting:

function fplotting(list of parameters)
for i = 1:(end)
    (do the animation plot)
end

here is the loop where I want to stop when I press the reset button. coz if anyone press Ctrl+C when the animation is undergoing (looping), it will give an error and the exe file will crash and requires reopening it. So what I want it be able to stop it when someone press the reset button.

If I use persistent, the new value, after I press the reset button, it will not pass into the existing loop and break the loop... looks like I am looking for a way to update the parameter after I have change it outside the function

Thanks


Use exit or quit to exit the program

To stop the loop from running, you can use break or return from a function..


Your best bet might be to use a PERSISTENT variable whose state you check at each iteration of the loop in function 2, and whose value you modify in the "reset"-callback.

reset callback

function reset_callback(hObj,eventdata,handles)
   %# "activate" persistent variable
   persistent breakThis
   %# set it to 1
   breakThis = true;

function with the loop

function functionWithTheLoop(someInput)
   ...
   %# "activate persistent variable
   persistent breakThis
   ...
   %# start loop
   for iter = 1:numOfIterations
      %# check whether you need to break out of the loop
      if breakThis
         %# reset the function value
         breakThis = false;
         break;
      end    
      ...   
   end


You can use application data information, i.e. setappdata and getappdata:

% animation loop function
function loop(hFig)
   setappdata(hFig, 'run', true);   % here or somewhere else...
   while(getappdata(hFig, 'run'))
      plot(....);
   end
end

% GUI stop-loop btn callback
function stop(hFig)
   setappdata(hFig, 'run', false);
end

I guess that would work.

0

精彩评论

暂无评论...
验证码 换一张
取 消