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

Thread Subject:
continue from pause in GUI

Subject: continue from pause in GUI

From: Sarah xxx

Date: 11 May, 2010 11:30:07

Message: 1 of 6

Hi!
I've a problem with the pause button. It makes an error that there are not enough variables but the mail thing is that I would like to continue after the pause butten with the continue button.
Any ideas? cheers for your help!

function [sol, out1] = gatsp(sol,options);
p=sol(1:(length(sol)-1));
sol;
out1 = fittness(p);


function [out1] = fittness(p);

    %xlulu= [224 334 655 755 1022 3677 4000 2222 3564 1000 348 122 555 4033 1045 999 2467 4081 2333 67 9 11 329 2385 2445 41 3222 3001 1999 999 1700 2789 1 4556 123 2852 3333];
    xlulu = 0:100:3600;
    x=(1:1:37) -18;
    xlulu= 10.*(x.^2);
    out1 = -1*sum(abs(xlulu-p));

    % %%%%%%2000 iterations initpop,[1e-6initpop,[1e-6
    % out1=[0.3743 2.7555 0.9139 0.7558 1.0270 3.6714 3.8461 0.5346 3.2557 0.8361 0.5183 2.6095 0.3801 4.0299 1.2277 1.0380 2.3479 4.6759 2.4330 0 0.1136 0.2867 0.5546 2.4295 2.5068 4.2904 0.4268 2.9739 1.9207 1.0489 2.0876 2.7739 0.0029 4.5095 0.0895 2.8599 3.1899];
    % %%%%%%2000 iterations initpop,[1e-9
    % out=[0.5299 0.2466 0.7711 0.6777 0.8473 3.6580 3.9831 2.3038 3.4581 1.0579 0.5088 0.0232 0.7274 3.8657 1.0000 0 2.3265 4.6404 2.3910 0.1592 0.1223 0.0002 0.3469 2.5753 1.1724 4.1586 3.2105 2.3443 2.0636 1.0269 1.7456 1.5622 0.0000 4.5362 0.1152 2.7134 3.1643];
    % out1 = out1.*1000
    %figure(1)
    global out
    out = [out; out1];
  
    %drawnow
    subplot(2,1,1);
    plt1 = plot(out,'.-black');
    hold on
    %plt2 = plot(j, out1,'.-r');
    hold off
    
    
    
    drawnow
    subplot(2,1,2);
    plt3 = plot(xlulu,'.b');
    hold on;
    plt4 = plot(p,'.-g');
    hold off

    global A
    global B
    global C
    global D

    if (A|B)
        dbstop
        %delete
    end

    if (A|C)
       pause on
          pause
% save filename p xlulu out1
% dbstop
% waitfor(hcontinue, 'Value', 1)
% pause (inf)
    end
    
    if (A|D)
        pause on
        pause off
   
    end
    
end




function sa_gui
% close all
% clear all
    % Initialize and hide the GUI as it is being constructed.
    f = figure('Visible','off','Position',[700,200,500,500]);

    % Construct the components.
    hstart = uicontrol('Style','pushbutton',...
                     'String','Start','Position',[70,240,70,25],...
                     'Callback',{@startbutton_Callback});
    hstop = uicontrol('Style','pushbutton',...
                     'String','Stop','Position',[170,240,70,25],...
                     'Callback',{@stopbutton_Callback});
    hpause = uicontrol('Style','pushbutton',...
                    'String','Pause','Position',[270,240,70,25],...
                    'Callback',{@pausebutton_Callback});
    hcontinue = uicontrol('Style','pushbutton','Value', 1, ...
                    'String','Continue','Position',[370,240,70,25],...
                    'Callback',{@continuebutton_Callback});
    %ha = axes('Parent',f,'Units','pixels','Position',[40,285,200,185]);
    %haFitness = axes('Units','pixels','Position',[40,285,200,185]);
    help sa_gui
   
   % Initialize the GUI.
   % Change units to normalized so components resize automatically.
   set([f,hstart, hstop, hpause, hcontinue],...
       'Units','normalized');
  
   % Assign the GUI a name to appear in the window title.
   set(f,'Name','Simple GUI')
   % Move the GUI to the center of the screen.
   movegui(f,'center')
   % Make the GUI visible.
   set(f,'Visible','on');
       
    function startbutton_Callback(source,eventdata)
        
        global A
        global B
        global C
        global D
        global out
        B = [0];
        A = [0];
        C = [0];
        D = [0];
        out = [];

    x=(1:1:38) -18;
    xlulu= 10.*(x.^2);
    min = xlulu -1500;
    max = xlulu + 1500;

    %min=zeros(38,1);
    %max=3800.*ones(38,1);
    bounds=[min(1) max(1);min(2) max(2);min(3) max(3);min(4) max(4);min(5) max(5);min(6) max(6);min(7) max(7);min(8) max(8);min(9) max(9);min(10) max(10);min(11) max(11);min(12) max(12);min(13) max(13);min(14) max(14);min(15) max(15);min(16) max(16);min(17) max(17);min(18) max(18);min(19) max(19);min(20) max(20);min(21) max(22);min(23) max(23);min(24) max(24);min(25) max(25);min(26) max(26);min(27) max(27);min(28) max(28);min(29) max(29);min(30) max(30);min(31) max(31);min(32) max(32);min(33) max(33);min(34) max(34);min(35) max(35);min(36) max(36);min(37) max(37);min(38) max(38)];

    initpop=initializega(6,bounds,'gatsp');
    %initpop=(100*ones(38,1));
    [x endPop bpop trace] = ga(bounds,'gatsp',[],initpop,[1e-6 1 0],'maxGenTerm',150);
   
    end

    function stopbutton_Callback(source,eventdata)
        global A;
        global B;
        B = [1];
    end

   function pausebutton_Callback(source,eventdata)
       global A;
       global C;
       C = [1];
   end

   function continuebutton_Callback(source,eventdata)
       
       
% global p, global xlulu, global out1
       global A, global B, global C, global D, global out
       C = [0];
       D = [1];
%
%
% x=(1:1:38) -18;
% %xlulu= 10.*(x.^2);
% min = xlulu -1500;
% max = xlulu + 1500;
%
% %min=zeros(38,1);
% %max=3800.*ones(38,1);
% bounds=[min(1) max(1);min(2) max(2);min(3) max(3);min(4) max(4);min(5) max(5);min(6) max(6);min(7) max(7);min(8) max(8);min(9) max(9);min(10) max(10);min(11) max(11);min(12) max(12);min(13) max(13);min(14) max(14);min(15) max(15);min(16) max(16);min(17) max(17);min(18) max(18);min(19) max(19);min(20) max(20);min(21) max(22);min(23) max(23);min(24) max(24);min(25) max(25);min(26) max(26);min(27) max(27);min(28) max(28);min(29) max(29);min(30) max(30);min(31) max(31);min(32) max(32);min(33) max(33);min(34) max(34);min(35) max(35);min(36) max(36);min(37) max(37);min(38) max(38)];
%
% initpop=initializega(6,bounds,'gatsp');
% %initpop=(100*ones(38,1));
% [x endPop bpop trace] = ga(bounds,'gatsp',[],initpop,[1e-6 1 0],'maxGenTerm',150);
   end

end

Subject: continue from pause in GUI

From: Sean

Date: 11 May, 2010 11:57:04

Message: 2 of 6

"Sarah xxx" <sarahSarah1052@gmx.de> wrote in message <hsbf3v$q0h$1@fred.mathworks.com>...
> Hi!
> I've a problem with the pause button. It makes an error that there are not enough variables but the mail thing is that I would like to continue after the pause butten with the continue button.
> Any ideas? cheers for your help!

What is the error specifically?
Have you looked at timer functions?
>>doc timer
It sounds like they're most likely what you want!

Good luck!

Subject: continue from pause in GUI

From: Sarah xxx

Date: 11 May, 2010 14:55:21

Message: 3 of 6

Thank you.
The problem is that I stop the GUI whithin a loop and that I also want to pause the loop or the plotted function which shows a optimization algorithm and then go on with the last found variables. I need the pause and continue button in the GUI and it should pause and continue if I press the button and not in a several time.
Any Idea?



"Sarah xxx" <sarahSarah1052@gmx.de> wrote in message <hsbf3v$q0h$1@fred.mathworks.com>...
> Hi!
> I've a problem with the pause button. It makes an error that there are not enough variables but the mail thing is that I would like to continue after the pause butten with the continue button.
> Any ideas? cheers for your help!
>
> function [sol, out1] = gatsp(sol,options);
> p=sol(1:(length(sol)-1));
> sol;
> out1 = fittness(p);
>
>
> function [out1] = fittness(p);
>
> %xlulu= [224 334 655 755 1022 3677 4000 2222 3564 1000 348 122 555 4033 1045 999 2467 4081 2333 67 9 11 329 2385 2445 41 3222 3001 1999 999 1700 2789 1 4556 123 2852 3333];
> xlulu = 0:100:3600;
> x=(1:1:37) -18;
> xlulu= 10.*(x.^2);
> out1 = -1*sum(abs(xlulu-p));
>
> % %%%%%%2000 iterations initpop,[1e-6initpop,[1e-6
> % out1=[0.3743 2.7555 0.9139 0.7558 1.0270 3.6714 3.8461 0.5346 3.2557 0.8361 0.5183 2.6095 0.3801 4.0299 1.2277 1.0380 2.3479 4.6759 2.4330 0 0.1136 0.2867 0.5546 2.4295 2.5068 4.2904 0.4268 2.9739 1.9207 1.0489 2.0876 2.7739 0.0029 4.5095 0.0895 2.8599 3.1899];
> % %%%%%%2000 iterations initpop,[1e-9
> % out=[0.5299 0.2466 0.7711 0.6777 0.8473 3.6580 3.9831 2.3038 3.4581 1.0579 0.5088 0.0232 0.7274 3.8657 1.0000 0 2.3265 4.6404 2.3910 0.1592 0.1223 0.0002 0.3469 2.5753 1.1724 4.1586 3.2105 2.3443 2.0636 1.0269 1.7456 1.5622 0.0000 4.5362 0.1152 2.7134 3.1643];
> % out1 = out1.*1000
> %figure(1)
> global out
> out = [out; out1];
>
> %drawnow
> subplot(2,1,1);
> plt1 = plot(out,'.-black');
> hold on
> %plt2 = plot(j, out1,'.-r');
> hold off
>
>
>
> drawnow
> subplot(2,1,2);
> plt3 = plot(xlulu,'.b');
> hold on;
> plt4 = plot(p,'.-g');
> hold off
>
> global A
> global B
> global C
> global D
>
> if (A|B)
> dbstop
> %delete
> end
>
> if (A|C)
> pause on
> pause
> % save filename p xlulu out1
> % dbstop
> % waitfor(hcontinue, 'Value', 1)
> % pause (inf)
> end
>
> if (A|D)
> pause on
> pause off
>
> end
>
> end
>
>
>
>
> function sa_gui
> % close all
> % clear all
> % Initialize and hide the GUI as it is being constructed.
> f = figure('Visible','off','Position',[700,200,500,500]);
>
> % Construct the components.
> hstart = uicontrol('Style','pushbutton',...
> 'String','Start','Position',[70,240,70,25],...
> 'Callback',{@startbutton_Callback});
> hstop = uicontrol('Style','pushbutton',...
> 'String','Stop','Position',[170,240,70,25],...
> 'Callback',{@stopbutton_Callback});
> hpause = uicontrol('Style','pushbutton',...
> 'String','Pause','Position',[270,240,70,25],...
> 'Callback',{@pausebutton_Callback});
> hcontinue = uicontrol('Style','pushbutton','Value', 1, ...
> 'String','Continue','Position',[370,240,70,25],...
> 'Callback',{@continuebutton_Callback});
> %ha = axes('Parent',f,'Units','pixels','Position',[40,285,200,185]);
> %haFitness = axes('Units','pixels','Position',[40,285,200,185]);
> help sa_gui
>
> % Initialize the GUI.
> % Change units to normalized so components resize automatically.
> set([f,hstart, hstop, hpause, hcontinue],...
> 'Units','normalized');
>
> % Assign the GUI a name to appear in the window title.
> set(f,'Name','Simple GUI')
> % Move the GUI to the center of the screen.
> movegui(f,'center')
> % Make the GUI visible.
> set(f,'Visible','on');
>
> function startbutton_Callback(source,eventdata)
>
> global A
> global B
> global C
> global D
> global out
> B = [0];
> A = [0];
> C = [0];
> D = [0];
> out = [];
>
> x=(1:1:38) -18;
> xlulu= 10.*(x.^2);
> min = xlulu -1500;
> max = xlulu + 1500;
>
> %min=zeros(38,1);
> %max=3800.*ones(38,1);
> bounds=[min(1) max(1);min(2) max(2);min(3) max(3);min(4) max(4);min(5) max(5);min(6) max(6);min(7) max(7);min(8) max(8);min(9) max(9);min(10) max(10);min(11) max(11);min(12) max(12);min(13) max(13);min(14) max(14);min(15) max(15);min(16) max(16);min(17) max(17);min(18) max(18);min(19) max(19);min(20) max(20);min(21) max(22);min(23) max(23);min(24) max(24);min(25) max(25);min(26) max(26);min(27) max(27);min(28) max(28);min(29) max(29);min(30) max(30);min(31) max(31);min(32) max(32);min(33) max(33);min(34) max(34);min(35) max(35);min(36) max(36);min(37) max(37);min(38) max(38)];
>
> initpop=initializega(6,bounds,'gatsp');
> %initpop=(100*ones(38,1));
> [x endPop bpop trace] = ga(bounds,'gatsp',[],initpop,[1e-6 1 0],'maxGenTerm',150);
>
> end
>
> function stopbutton_Callback(source,eventdata)
> global A;
> global B;
> B = [1];
> end
>
> function pausebutton_Callback(source,eventdata)
> global A;
> global C;
> C = [1];
> end
>
> function continuebutton_Callback(source,eventdata)
>
>
> % global p, global xlulu, global out1
> global A, global B, global C, global D, global out
> C = [0];
> D = [1];
> %
> %
> % x=(1:1:38) -18;
> % %xlulu= 10.*(x.^2);
> % min = xlulu -1500;
> % max = xlulu + 1500;
> %
> % %min=zeros(38,1);
> % %max=3800.*ones(38,1);
> % bounds=[min(1) max(1);min(2) max(2);min(3) max(3);min(4) max(4);min(5) max(5);min(6) max(6);min(7) max(7);min(8) max(8);min(9) max(9);min(10) max(10);min(11) max(11);min(12) max(12);min(13) max(13);min(14) max(14);min(15) max(15);min(16) max(16);min(17) max(17);min(18) max(18);min(19) max(19);min(20) max(20);min(21) max(22);min(23) max(23);min(24) max(24);min(25) max(25);min(26) max(26);min(27) max(27);min(28) max(28);min(29) max(29);min(30) max(30);min(31) max(31);min(32) max(32);min(33) max(33);min(34) max(34);min(35) max(35);min(36) max(36);min(37) max(37);min(38) max(38)];
> %
> % initpop=initializega(6,bounds,'gatsp');
> % %initpop=(100*ones(38,1));
> % [x endPop bpop trace] = ga(bounds,'gatsp',[],initpop,[1e-6 1 0],'maxGenTerm',150);
> end
>
> end

Subject: continue from pause in GUI

From: Sean

Date: 11 May, 2010 15:32:05

Message: 4 of 6

"Sarah xxx" <sarahSarah1052@gmx.de> wrote in message <hsbr4p$kkq$1@fred.mathworks.com>...
> Thank you.
> The problem is that I stop the GUI whithin a loop and that I also want to pause the loop or the plotted function which shows a optimization algorithm and then go on with the last found variables. I need the pause and continue button in the GUI and it should pause and continue if I press the button and not in a several time.
> Any Idea?
>

One way that could probably work as long as you don't want to do other things while it's paused.

Become familiar with these:
>>help getappdata
>>help setappdata

pseudo code
continue button callback()
setappdata(0,'FLAG', true)

pause button callback ()
setappdata(0,'FLAG',false);

loop
     FLAG = getappdata(0,'FLAG');
     while FLAG
           FLAG = getappdata(0,'FLAG')
     end
     optimize()
     plot()
loopend

Every iteration of optimize and plot will be tested to see if the pause button is clicked and will hold it until continue is clicked.

Subject: continue from pause in GUI

From: Sarah xxx

Date: 13 May, 2010 09:53:03

Message: 5 of 6

Thank you Sean.
I found a way to stop and continue with waitfor

    while (C==1)
        global hcontinue
        pause(0.1)
        waitfor(hcontinue,'Value')
        %break
    end

it works, but the continue button works only once. Maybe I have to write another Loop? I can press the pause button and it stops, then the continue button, the plot continues, after that the pausebutton pauses the plot again but continue doesn't work anymore. Do you know wat the problem coul be?

cheers, Sarah
"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <hsbt9l$d83$1@fred.mathworks.com>...
> "Sarah xxx" <sarahSarah1052@gmx.de> wrote in message <hsbr4p$kkq$1@fred.mathworks.com>...
> > Thank you.
> > The problem is that I stop the GUI whithin a loop and that I also want to pause the loop or the plotted function which shows a optimization algorithm and then go on with the last found variables. I need the pause and continue button in the GUI and it should pause and continue if I press the button and not in a several time.
> > Any Idea?
> >
>
> One way that could probably work as long as you don't want to do other things while it's paused.
>
> Become familiar with these:
> >>help getappdata
> >>help setappdata
>
> pseudo code
> continue button callback()
> setappdata(0,'FLAG', true)
>
> pause button callback ()
> setappdata(0,'FLAG',false);
>
> loop
> FLAG = getappdata(0,'FLAG');
> while FLAG
> FLAG = getappdata(0,'FLAG')
> end
> optimize()
> plot()
> loopend
>
> Every iteration of optimize and plot will be tested to see if the pause button is clicked and will hold it until continue is clicked.

Subject: continue from pause in GUI

From: Sean

Date: 13 May, 2010 13:19:04

Message: 6 of 6

"Sarah xxx" <sarahSarah1052@gmx.de> wrote in message <hsgi5v$9q3$1@fred.mathworks.com>...
> Thank you Sean.
> I found a way to stop and continue with waitfor
>
> while (C==1)
> global hcontinue
> pause(0.1)
> waitfor(hcontinue,'Value')
> %break
> end
>
> it works, but the continue button works only once. Maybe I have to write another Loop? I can press the pause button and it stops, then the continue button, the plot continues, after that the pausebutton pauses the plot again but continue doesn't work anymore. Do you know wat the problem coul be?

Yes, that it to be expected. The problem I believe is that because it's a continuous while loop the button press function is being queued instead of run. If you insert a quick
>> drawnow;
it will check to see if anything's been queued. I made up a quick gui/function to show you this but your email address doesn't accept from unknown emails.
Here's the code from the gui function; the gui had 4 buttons: startButton, goButton, pauseButton, stopButton; each doing their corresponding action.


function goButton_Callback(hObject, eventdata, handles)
setappdata(0,'GO',1)
%go

% --- Executes on button press in pauseButton.
function pauseButton_Callback(hObject, eventdata, handles)
setappdata(0,'GO',0)
%pause;

% --- Executes on button press in startButton.
function startButton_Callback(hObject, eventdata, handles)

setappdata(0,'GO',1)
setappdata(0,'STOP',0)
startfun;
%Start for the first time

% --- Executes on button press in stopButton.
function stopButton_Callback(hObject, eventdata, handles)
setappdata(0,'STOP',1)
%Stop for good.

function startfun()
%actual function
    i = 0;
    STOP = getappdata(0,'STOP');
    GO = getappdata(0,'GO');
    while ~STOP
        drawnow;
        if GO
            pause(0.5); %Only so the screen doesn't blow up with Hello Worlds
            i = i+1 %shows i on every iteration so you can see that it doesn't reset
        end
        GO = getappdata(0,'GO');
        STOP = getappdata(0,'STOP');
   end

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us