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:
pause a timer object

Subject: pause a timer object

From: Jose

Date: 5 Jan, 2010 16:50:05

Message: 1 of 4

Hello everyone, I got two independent process running in the same matlab with the timer objects, t1 and t2, and I am interested to pause one of the process, not all.

The independent process run independently with the timers t1 and t2, given by:

t1 = timer('TimerFcn',@timerFun,'executionmode','fixedrate');

t2 = timer('TimerFcn',@timerFun2,'executionmode','fixedrate');

Pressing a button, given by:

hs1=uicontrol (gcf,'String','Pause','Position',[10,10,60,20],'Callback','pauset1');

It call the function pauset1:

function pauset1(obj, event, string_arg);
waitforbuttonpress
 return


The command "waitforbuttonpress" stop all the process 1 and 2, but I am only interested to pause the process 1.

Any ideas?

Thnaks in advance.

Jose.

Subject: pause a timer object

From: Matthew Whitaker

Date: 7 Jan, 2010 18:13:04

Message: 2 of 4

"Jose " <jose.l.vega@gmail.com> wrote in message <hhvqjt$964$1@fred.mathworks.com>...
> Hello everyone, I got two independent process running in the same matlab with the timer objects, t1 and t2, and I am interested to pause one of the process, not all.
>
> The independent process run independently with the timers t1 and t2, given by:
>
> t1 = timer('TimerFcn',@timerFun,'executionmode','fixedrate');
>
> t2 = timer('TimerFcn',@timerFun2,'executionmode','fixedrate');
>
> Pressing a button, given by:
>
> hs1=uicontrol (gcf,'String','Pause','Position',[10,10,60,20],'Callback','pauset1');
>
> It call the function pauset1:
>
> function pauset1(obj, event, string_arg);
> waitforbuttonpress
> return
>
>
> The command "waitforbuttonpress" stop all the process 1 and 2, but I am only interested to pause the process 1.
>
> Any ideas?
>
> Thnaks in advance.
>
> Jose.


You can stop and start the timers. Here is some demo code:

function pauseDemo
    %create a figure with a couple of labels
    f = figure('CloseRequestFcn',@figureClose);
    txtProcess(1)= uicontrol('Style','text',...
        'Parent',f,...
              'Units','normalized',...
              'Position',[0.3,0.7,0.4,0.2],...
              'BackgroundColor','w');
          
    txtProcess(2) = uicontrol('Style','text',...
        'Parent',f,...
              'Units','normalized',...
              'Position',[0.3,0.2,0.4,0.2],...
              'BackgroundColor','w');
          
     uicontrol('Style','pushbutton',...
               'Parent',f,...
               'Units','normalized',...
               'Position',[0.72,0.7,0.2,0.2],...
               'String','Pause Process 1',...
               'Callback',@cmdPause1);
          
    process1Pause = false;
    
    t(1)= timer('TimerFcn',@timerFun,'executionmode','fixedrate');

    t(2) = timer('TimerFcn',@timerFun2,'executionmode','fixedrate');
    start(t);
    function cmdPause1(hObject,varargin)
        process1Pause = ~process1Pause;
        if process1Pause
            stop(t(1));
            set(hObject,'String','Start Process 1');
            set(txtProcess(1),'String','Paused');
        else
            start(t(1));
            set(hObject,'String','Pause Process 1');
        end %if
        drawnow;
    end %cmdPause1

    function timerFun(varargin)
            set(txtProcess(1),'String',datestr(now));
            drawnow;
    end %timerFun

    function timerFun2(varargin)
        set(txtProcess(2),'String',datestr(now));
        drawnow;
    end %timerFun2

    function figureClose(varargin)
        %need to clean up timers before exiting
        stop(t);
        delete(t);
        delete(f);
    end
end %pauseDemo

Hope this Helps
MattW

Subject: pause a timer object

From: Jose

Date: 8 Jan, 2010 11:19:03

Message: 3 of 4

"Matthew Whitaker" <mattlwhitaker@REMOVEgmail.com> wrote in message <hi587g$d4j$1@fred.mathworks.com>...
> "Jose " <jose.l.vega@gmail.com> wrote in message <hhvqjt$964$1@fred.mathworks.com>...
> > Hello everyone, I got two independent process running in the same matlab with the timer objects, t1 and t2, and I am interested to pause one of the process, not all.
> >
> > The independent process run independently with the timers t1 and t2, given by:
> >
> > t1 = timer('TimerFcn',@timerFun,'executionmode','fixedrate');
> >
> > t2 = timer('TimerFcn',@timerFun2,'executionmode','fixedrate');
> >
> > Pressing a button, given by:
> >
> > hs1=uicontrol (gcf,'String','Pause','Position',[10,10,60,20],'Callback','pauset1');
> >
> > It call the function pauset1:
> >
> > function pauset1(obj, event, string_arg);
> > waitforbuttonpress
> > return
> >
> >
> > The command "waitforbuttonpress" stop all the process 1 and 2, but I am only interested to pause the process 1.
> >
> > Any ideas?
> >
> > Thnaks in advance.
> >
> > Jose.
>
>
> You can stop and start the timers. Here is some demo code:
>
> function pauseDemo
> %create a figure with a couple of labels
> f = figure('CloseRequestFcn',@figureClose);
> txtProcess(1)= uicontrol('Style','text',...
> 'Parent',f,...
> 'Units','normalized',...
> 'Position',[0.3,0.7,0.4,0.2],...
> 'BackgroundColor','w');
>
> txtProcess(2) = uicontrol('Style','text',...
> 'Parent',f,...
> 'Units','normalized',...
> 'Position',[0.3,0.2,0.4,0.2],...
> 'BackgroundColor','w');
>
> uicontrol('Style','pushbutton',...
> 'Parent',f,...
> 'Units','normalized',...
> 'Position',[0.72,0.7,0.2,0.2],...
> 'String','Pause Process 1',...
> 'Callback',@cmdPause1);
>
> process1Pause = false;
>
> t(1)= timer('TimerFcn',@timerFun,'executionmode','fixedrate');
>
> t(2) = timer('TimerFcn',@timerFun2,'executionmode','fixedrate');
> start(t);
> function cmdPause1(hObject,varargin)
> process1Pause = ~process1Pause;
> if process1Pause
> stop(t(1));
> set(hObject,'String','Start Process 1');
> set(txtProcess(1),'String','Paused');
> else
> start(t(1));
> set(hObject,'String','Pause Process 1');
> end %if
> drawnow;
> end %cmdPause1
>
> function timerFun(varargin)
> set(txtProcess(1),'String',datestr(now));
> drawnow;
> end %timerFun
>
> function timerFun2(varargin)
> set(txtProcess(2),'String',datestr(now));
> drawnow;
> end %timerFun2
>
> function figureClose(varargin)
> %need to clean up timers before exiting
> stop(t);
> delete(t);
> delete(f);
> end
> end %pauseDemo
>
> Hope this Helps
> MattW

Thank you so much Matt!!! The code is great!!

Subject: pause a timer object

From: Jose

Date: 8 Jan, 2010 14:11:02

Message: 4 of 4

"Jose " <jose.l.vega@gmail.com> wrote in message <hi74b7$fql$1@fred.mathworks.com>...
> "Matthew Whitaker" <mattlwhitaker@REMOVEgmail.com> wrote in message <hi587g$d4j$1@fred.mathworks.com>...
> > "Jose " <jose.l.vega@gmail.com> wrote in message <hhvqjt$964$1@fred.mathworks.com>...
> > > Hello everyone, I got two independent process running in the same matlab with the timer objects, t1 and t2, and I am interested to pause one of the process, not all.
> > >
> > > The independent process run independently with the timers t1 and t2, given by:
> > >
> > > t1 = timer('TimerFcn',@timerFun,'executionmode','fixedrate');
> > >
> > > t2 = timer('TimerFcn',@timerFun2,'executionmode','fixedrate');
> > >
> > > Pressing a button, given by:
> > >
> > > hs1=uicontrol (gcf,'String','Pause','Position',[10,10,60,20],'Callback','pauset1');
> > >
> > > It call the function pauset1:
> > >
> > > function pauset1(obj, event, string_arg);
> > > waitforbuttonpress
> > > return
> > >
> > >
> > > The command "waitforbuttonpress" stop all the process 1 and 2, but I am only interested to pause the process 1.
> > >
> > > Any ideas?
> > >
> > > Thnaks in advance.
> > >
> > > Jose.
> >
> >
> > You can stop and start the timers. Here is some demo code:
> >
> > function pauseDemo
> > %create a figure with a couple of labels
> > f = figure('CloseRequestFcn',@figureClose);
> > txtProcess(1)= uicontrol('Style','text',...
> > 'Parent',f,...
> > 'Units','normalized',...
> > 'Position',[0.3,0.7,0.4,0.2],...
> > 'BackgroundColor','w');
> >
> > txtProcess(2) = uicontrol('Style','text',...
> > 'Parent',f,...
> > 'Units','normalized',...
> > 'Position',[0.3,0.2,0.4,0.2],...
> > 'BackgroundColor','w');
> >
> > uicontrol('Style','pushbutton',...
> > 'Parent',f,...
> > 'Units','normalized',...
> > 'Position',[0.72,0.7,0.2,0.2],...
> > 'String','Pause Process 1',...
> > 'Callback',@cmdPause1);
> >
> > process1Pause = false;
> >
> > t(1)= timer('TimerFcn',@timerFun,'executionmode','fixedrate');
> >
> > t(2) = timer('TimerFcn',@timerFun2,'executionmode','fixedrate');
> > start(t);
> > function cmdPause1(hObject,varargin)
> > process1Pause = ~process1Pause;
> > if process1Pause
> > stop(t(1));
> > set(hObject,'String','Start Process 1');
> > set(txtProcess(1),'String','Paused');
> > else
> > start(t(1));
> > set(hObject,'String','Pause Process 1');
> > end %if
> > drawnow;
> > end %cmdPause1
> >
> > function timerFun(varargin)
> > set(txtProcess(1),'String',datestr(now));
> > drawnow;
> > end %timerFun
> >
> > function timerFun2(varargin)
> > set(txtProcess(2),'String',datestr(now));
> > drawnow;
> > end %timerFun2
> >
> > function figureClose(varargin)
> > %need to clean up timers before exiting
> > stop(t);
> > delete(t);
> > delete(f);
> > end
> > end %pauseDemo
> >
> > Hope this Helps
> > MattW
>
> Thank you so much Matt!!! The code is great!!

Hello Matt,
I got the next mistake in my code when I try to stop t(1) and t(2)
??? Error while evaluating TimerFcn for timer 'timer-22'

It looks like the program still try to calling the function TimerFcn before
I stop the timer object t(1), and t(2)...the program is stopped but I would like to end the program without this error.
Some suggestion about it?
Thanks in advance,

Jose.

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