Got Questions? Get Answers.
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:
Utilizing a STOP button on GUI

Subject: Utilizing a STOP button on GUI

From: Kevin

Date: 3 Sep, 2009 13:33:04

Message: 1 of 8

I wrote a script to collect data from a data acquisition card. I need to wrap a gui around this code and the problem I am running into is the ability to stop the code when the user wants. The non-gui code runs for a finite amount of time and same for the gui now. Anyway, my gui consists of two plot axes and two buttons. One button is to begin collection and the other is to stop collection. Inside the Run button callback, my code enters a while loop to perform the data capture and display. I would like to stop capture when the stop button is pressed, therefore execting its callback. I can stop the actual data collection object but since the while loop is still running in the other callback, I get errors because data is trying to be grabbed from a stopped data acquisiiton object. Both interruptible properties are set to on. Also, I have put statements in the while loop to check
the value of the stop button and while this is value is being polled, it never detects a change when the button is pressed.

How can I enable the action of the stop button to break the while loop exectuion?

Thank you,
Kevin

Subject: Utilizing a STOP button on GUI

From: Geoffrey Akien

Date: 3 Sep, 2009 14:28:02

Message: 2 of 8

Don't use while loops - use timers instead.

Subject: Utilizing a STOP button on GUI

From: Kevin

Date: 3 Sep, 2009 14:45:20

Message: 3 of 8

Can you expand on your comment a little? I am a little unsure how to implement them especially in my case.

I want to capture data continually until the stop button is pressed. Since I am capturing data at a high sample rate (around 50kS/s), I am only plotting every few thousand points.

-Kevin

Subject: Utilizing a STOP button on GUI

From: Matt Fig

Date: 3 Sep, 2009 14:56:06

Message: 4 of 8

To make the stop button check work you need to issue a drawnow or pause(.00001) statement. This forces the graphics queue to flush.

Subject: Utilizing a STOP button on GUI

From: Kevin

Date: 3 Sep, 2009 15:09:03

Message: 5 of 8

Thanks for the tip, unfortunately I already have drawnow statements after the plot statements. Do I need them elsewhere? Is there a way to post the code up here?

Here is the a piece of the structure. The run button callback calls a datacapture function; Listed below. Both plot statements have drawnows listed after them.

function handles=dataCapture(handles)
ai=handles.ai;
preview=handles.preview;
NFFT=handles.NFFT;f=handles.f;
i=1;
handles.stopButtonState=0;
while ai.SamplesAcquired < ai.SamplesPerTrigger
    stopButtonState=get(handles.StopCapturePushButton,'Value');
    if stopButtonState==1
        stopButtonState
        stop(ai);delete(ai);display('Stopping device');
        return;
    end
    if (ai.SamplesAcquired==0)
        stop(ai);delete(ai);display('Not acquiring data, stopping device');
        return;
    end
    while ai.SamplesAcquired < preview*i;
        stopButtonState=get(handles.StopCapturePushButton,'Value');
        if stopButtonState==1
            stopButtonState
            stop(ai);delete(ai);display('Stopping device in while loop');
            return;
        end
        % display('yes')
        if (ai.SamplesAcquired==0)
            stop(ai);delete(ai);clear ai;display('Not acquiring data, stopping device');
            return;
        end
    end
    data= peekdata(ai,preview);handles.data=data;
    % assignin('base','data',data);
    
    set(handles.figure1,'CurrentAxes',handles.RawDataAxes);hold off;cla(handles.RawDataAxes);
    hold on;plot(handles.RawDataAxes,1:preview/2,data(1:preview/2,:));drawnow;legend(handles.RawDataAxes,'Q1','Q2','Q3','Q4');
    
    % Performing and plotting FFT
    handles=PerformAndPlotFFT(handles);
    i=i+1;
end
display('done')

Subject: Utilizing a STOP button on GUI

From: Matt Fig

Date: 3 Sep, 2009 15:22:04

Message: 6 of 8

I assumed you were using a toggle button, not a pushbutton. The pushbutton value is only 1 while the mouse is holding it down. If you want to use a pushbutton, you could set some value in the handles structure (say handles.stop = 1) in the pushbutton callback, then check this value immediately after a pause(.0001). Then on cleanup reset the handles.stop to zero.

Subject: Utilizing a STOP button on GUI

From: Kevin

Date: 3 Sep, 2009 16:11:00

Message: 7 of 8

Thank you for pointing me to the toggle button. I have implemented the toggle and it appears to be working. I had tried previously setting a value based on the pushbutton state but I never checked it after a pause.

thank you.

Subject: Utilizing a STOP button on GUI

From: stg2031991@gmail.com

Date: 21 Jan, 2013 10:08:23

Message: 8 of 8

could you help me out with this? i want to use the toggle button to stop the video acquisition once started.i keep getting a MULTIPLE VIDEOINPUT error when i press the stop toggle. how do i correct it?

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