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:
stop/pause plot signal with toggle button

Subject: stop/pause plot signal with toggle button

From: zack00

Date: 26 Mar, 2013 08:32:20

Message: 1 of 9

Hi
I have a 2 axes in my GUI with 2 different running signals..and 2 toggle buttons

If toggle button = 1 signal is running and plotting...and if toggle button = 0 i want to stop/pause plotting this signal..and it is a problem for me

How can I do it?

thanks for answer

Subject: stop/pause plot signal with toggle button

From: Nasser M. Abbasi

Date: 26 Mar, 2013 09:17:03

Message: 2 of 9

On 3/26/2013 3:32 AM, zack00 wrote:
> Hi
> I have a 2 axes in my GUI with 2 different running signals..and 2 toggle buttons
>
> If toggle button = 1 signal is running and plotting...and if toggle
>button = 0 i want to stop/pause plotting this signal..and it is a problem for me
>
> How can I do it?
>
> thanks for answer
>

May be you use a call back to flip which plot to make?

something like this:

-------------------------------------------------------
function foo()

stopIt = false;
signalToPlot = 0;
t = 0:.2:12*pi;
N = length(t);
k =0;

set(0,'Units','Normalized');
f=figure('Units','normalized','Position',[.3 .3 .42 .42]);
h1=axes('Parent',f,'Units','normalized','Position',[.2 .2 .60 .6]);
h1Btn=uicontrol('Units','Normalized','Parent',f,...
     'Style', 'pushbutton',...
     'Position', [.05 .05 .1 .1],...
     'String','toggle',...
     'Callback', @btn1Callback,'FontSize',12);
h2Btn=uicontrol('Units','Normalized','Parent',f,...
     'Style', 'pushbutton',...
     'Position', [.15 .05 .1 .1],...
     'String','quit',...
     'Callback', @btn2Callback,'FontSize',12);
hold on;
xlim([0 12*pi]);
ylim([-1.1 1.1]);
grid;

while not(stopIt)
     cla(h1);
     k = mod(k+1,N);
     if signalToPlot
        plot(t(1:k),sin(t(1:k)),'r.-');
        title('sin(x)');
     else
        plot(t(1:k),cos(t(1:k)),'k.-');
        title('cos(x)');
     end
     pause(.1);
end

     function btn1Callback(hObject,eventdata)
         signalToPlot = not(signalToPlot);
     end

     function btn2Callback(hObject,eventdata)
         stopIt=true;
     end

close all;
end

----------------------------

Subject: stop/pause plot signal with toggle button

From: Nasser M. Abbasi

Date: 26 Mar, 2013 09:34:24

Message: 3 of 9

On 3/26/2013 4:17 AM, Nasser M. Abbasi wrote:
> On 3/26/2013 3:32 AM, zack00 wrote:
>> Hi
>> I have a 2 axes in my GUI with 2 different running signals..and 2 toggle buttons
>>
>> If toggle button = 1 signal is running and plotting...and if toggle
>> button = 0 i want to stop/pause plotting this signal..and it is a problem for me
>>
>> How can I do it?
>>
>> thanks for answer
>>
>
> May be you use a call back to flip which plot to make?
>
> something like this:
>

This version uses 2 axes instead of one. (just noticed you
wanted 2 axes)

---------------------------------------
function foo()

stopIt = false;
signalToPlot = 0;
t = 0:.2:4*pi;
N = length(t);
k1 =0;
k2 =0;

set(0,'Units','Normalized');
f=figure('Units','normalized','Position',[.3 .3 .42 .42]);
h1=axes('Parent',f,'Units','normalized','Position',[.1 .2 .30 .6]);
h2=axes('Parent',f,'Units','normalized','Position',[.5 .2 .30 .6]);
h1Btn=uicontrol('Units','Normalized','Parent',f,...
     'Style', 'pushbutton',...
     'Position', [.05 .05 .1 .1],...
     'String','toggle',...
     'Callback', @btn1Callback,'FontSize',12);
h2Btn=uicontrol('Units','Normalized','Parent',f,...
     'Style', 'pushbutton',...
     'Position', [.15 .05 .1 .1],...
     'String','quit',...
     'Callback', @btn2Callback,'FontSize',12);

while not(stopIt)
     
     if signalToPlot
         k1 = mod(k1+1,N);
         set(f,'CurrentAxes',h1);
         plot(t(1:k1),sin(t(1:k1)),'r.-');
         title('sin(x)');
     else
         k2 = mod(k2+1,N);
         set(f,'CurrentAxes',h2);
         plot(t(1:k2),cos(t(1:k2)),'k.-');
         title('cos(x)');
     end
     xlim([0 4*pi]);
     ylim([-1.1 1.1]);
     grid;
     
     drawnow;
     pause(.1);
end

     function btn1Callback(hObject,eventdata)
         signalToPlot = not(signalToPlot);
     end

     function btn2Callback(hObject,eventdata)
         stopIt=true;
     end

close all;
end
---------------------

Subject: stop/pause plot signal with toggle button

From: zack00

Date: 26 Mar, 2013 10:23:42

Message: 4 of 9

thanks for help..second choice is good, but i want to stop and start first axe or second axe independently

Subject: stop/pause plot signal with toggle button

From: Nasser M. Abbasi

Date: 26 Mar, 2013 10:43:51

Message: 5 of 9

On 3/26/2013 5:23 AM, zack00 wrote:
> thanks for help..second choice is good, but i want to stop and
>start first axe or second axe independently
>


Just add another button and separate flags to control each axes on its own.
The idea is the same. Using callbacks and some state flag you can
make any logic you want.

something like

-------------------------
function foo()

stopIt = false;
signal1ToPlot = 1;
signal2ToPlot = 1;
t = 0:.2:4*pi;
N = length(t);
k1 =0;
k2 =0;

set(0,'Units','Normalized');
f=figure('Units','normalized','Position',[.3 .3 .42 .42]);
h1=axes('Parent',f,'Units','normalized','Position',[.1 .2 .30 .6]);
h2=axes('Parent',f,'Units','normalized','Position',[.5 .2 .30 .6]);
h1Btn=uicontrol('Units','Normalized','Parent',f,...
     'Style', 'pushbutton',...
     'Position', [.15 .05 .1 .1],...
     'String','toggle',...
     'Callback', @btn1Callback,'FontSize',12);
h2Btn=uicontrol('Units','Normalized','Parent',f,...
     'Style', 'pushbutton',...
     'Position', [.55 .05 .1 .1],...
     'String','toggle',...
     'Callback', @btn2Callback,'FontSize',12);

h3Btn=uicontrol('Units','Normalized','Parent',f,...
     'Style', 'pushbutton',...
     'Position', [.8 .05 .1 .1],...
     'String','quit',...
     'Callback', @btn3Callback,'FontSize',12);

while not(stopIt)
     
     if signal1ToPlot
         k1 = mod(k1+1,N);
         set(f,'CurrentAxes',h1);
         plot(t(1:k1),sin(t(1:k1)),'r.-');
         title('sin(x)');
         xlim([0 4*pi]);
         ylim([-1.1 1.1]);
         grid;
     end
     
     if signal2ToPlot
         k2 = mod(k2+1,N);
         set(f,'CurrentAxes',h2);
         plot(t(1:k2),cos(t(1:k2)),'k.-');
         title('cos(x)');
         xlim([0 4*pi]);
         ylim([-1.1 1.1]);
         grid;
     end
     
     
     drawnow;
     pause(.1);
end

     function btn1Callback(hObject,eventdata)
         signal1ToPlot = not(signal1ToPlot);
     end

     function btn2Callback(hObject,eventdata)
         signal2ToPlot = not(signal2ToPlot);
     end

     function btn3Callback(hObject,eventdata)
         stopIt=true;
     end

close all;
end

----------------------
--Nasser

Subject: stop/pause plot signal with toggle button

From: zack00

Date: 26 Mar, 2013 15:09:48

Message: 6 of 9

thank you..it works :)

Subject: stop/pause plot signal with toggle button

From: zack00

Date: 26 Mar, 2013 19:19:50

Message: 7 of 9

still small problem..If I open m-file I dont want to run signals..It display only interface and if I click on toggle button then signal is running and plotting in axe..how can I do it?

Subject: stop/pause plot signal with toggle button

From: zack00

Date: 26 Mar, 2013 19:22:03

Message: 8 of 9

and it has running infinitely and stop when I click on toggle button

Subject: stop/pause plot signal with toggle button

From: Nasser M. Abbasi

Date: 26 Mar, 2013 20:37:06

Message: 9 of 9

On 3/26/2013 2:19 PM, zack00 wrote:
> still small problem..If I open m-file I dont want to run signals..
>It display only interface and if I click on toggle button then signal is
>running and plotting in axe..how can I do it?
>

changes these

signal1ToPlot = 1;
signal2ToPlot = 1;

to

signal1ToPlot = 0;
signal2ToPlot = 0;

You really need to look at the code and see how it works.
This is meant as an example for you to learn from.

--Nasser

Tags for this Thread

No tags are associated with 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