How to stop data acquisition with a push button from GUI?

10 visualizaciones (últimos 30 días)
Laurynas
Laurynas el 13 de Mzo. de 2015
Comentada: Suleman Zafar el 26 de Sept. de 2016
Hello all, I've been struggling with this problem for the past three days and went through a ton of MathWorks threads but couldn't find the answer so I finally decided to post a question. I tried to explain the part of the code that I'm having problems with as well as I could so it would rise as little questions as possible.
Here's the code:
(the folded functions are actually full I just deleted it for ease of reading as I believe they are irrelevant as the code actually works fine)
function varargout = daq(varargin)
function daq_OpeningFcn(hObject, eventdata, handles, varargin)
function varargout = daq_OutputFcn(hObject, eventdata, handles)
%THESE NEXT FOUR FUNCTIONS ARE MY ANALOG TRIGGER
%This one executes on button "Initiate Listening" push in GUI
function initiate_Callback(hObject, eventdata, handles)
%it creates a figure
figure('Name','Chord C Tryouts','NumberTitle','off','MenuBar', 'None')
%creates a push button
btn = uicontrol('Style', 'pushbutton', 'String', 'Stop',...
'Position', [20 20 50 20],...
'Callback', 'stop(ai)');%This push button is suppose to stop
%the analog input object
%creates the analog input
ai = analoginput('winsound');
addchannel(ai,1);
ai.SamplesPerTrigger = Inf;
ai.TimerPeriod = 0.25;
ai.TimerFcn = {@ai_f}; %tells the trigger what function to use (ai_f)
%starts the analog input
start(ai);
function ai_f(hObject, eventdata, handles)
run = isrunning(hObject)%This line helps me easily determine if the analog
%input object is running (it shows run = 1 in
%matlab command prompt if analog input is running
[eventdata] =getdata(hObject,hObject.SamplesAvailable);
%Here it determines what to do if the trigger is triggered
if(ai_condition(eventdata))
bad_Callback %First it executes one of my calculating functions
stop(hObject) %then it stops the analog input object
pause(1.5) %waits for 1.5 seconds
rerun %and executes another function which starts the analog
%input object again
end
%I need to stop the analog input and start it again in a little while
%instead of just keeping it running because i need a brief period of
%time when the program is not aquiering data (not listening) right after
%the trigger is triggered
function condition = ai_condition(eventdata)
h = fft(eventdata); %
abs_h = abs(h); %
threshold = 100; %These are the conditions for the trigger to get
val = max(abs_h); %triggered. It calculates fft of analog input (the
%sound coming from my microphone) and then checks
if (val > threshold) %the magnitude of this signal.
%If max value of the magnitude (volume in my room)
condition = 1; %exceeds a predetermined value ("threshold" in this
%case) the trgigger gets triggered and does what is
else %told in the function above(ai_f)
condition = 0; %
end %
function rerun(hObject, eventdata, handles)
%This function simply starts the analog input again.
ai = analoginput('winsound');
addchannel(ai,1);
ai.SamplesPerTrigger = Inf;
ai.TimerPeriod = 0.25;
ai.TimerFcn = {@ai_f};
start(ai);
%These are the functions that does all the calculation for my program
%these work just fine
function bad_Callback(hObject, eventdata, handles)
function compare_Callback(hObject, eventdata, handles)
Almost everything in this code work perfectly. The analogue trigger reacts when I want it to react to the sound and the program works fine. It also stops the analog input and restarts it perfectly after 1,5 second as I want it to.
The problem I'm having is that the push button on the figure that's suppose to stop the data acquisition is not working. I tried replacing 'stop(ai)' with 'stop(hObject)', tried creating another function 'stop' and pointing the push button to it using '@stop' but nothing works.
I tried splitting the code into separate functions and executing 'ai = analoginput('winsound')' and 'start(ai)' directly in MatLab command window (not with a GUI push button from a function). In this case the variable ai appears in MatLab variables on the top right hand corner and the push button 'stop(ai)' in the GUI works, but unfortunately that is not acceptable for me as I will be making an executable file later on so I need everything to work from GUI.
Any help would be appreciated.
Thank you in advance.
  1 comentario
W Joey
W Joey el 17 de Jul. de 2015
Hi Laulynas, I used your code and modified it to run. But i found that the analog input will stop and never run again despite the rerun function. The error message shows‘Timeout expired before operation could complete'. Could you figure it out what's wrong with it? Thanks!

Iniciar sesión para comentar.

Respuesta aceptada

Laurynas
Laurynas el 14 de Mzo. de 2015
Finally I figured it out. If anyone comes by this post here's what I did:
At the point of the code where I define "ai = analoginput('winsound');" I added a line "assignin ('base','ai',ai)". This line makes the variable "ai" visible in matlab workspace and then a push button "btn = uicontrol('Style', 'pushbutton', 'String', 'Stop',... 'Position', [20 20 50 20],... 'Callback', 'stop(ai)');" works and stops the analog input.
I hope this helps someone. It took me days to think of this solution.
Good luck
  1 comentario
Suleman  Zafar
Suleman Zafar el 26 de Sept. de 2016
Hi Laurynes, I have the problem like your's and I tried the 2 commands you wrote above but it is not working in my case.Can you please help me with this? Best regards, Suleman

Iniciar sesión para comentar.

Más respuestas (1)

Laurynas
Laurynas el 13 de Mzo. de 2015
I took some screen shoots of the GUI. It might help to understand how it works.
In the last image you can see how I stop the analog input. When I take out the lines telling it to restart analog input (by commenting them) and save the code next trigger that occurs becomes the last as there's no commands starting the analog input again.
Stupid way to do it, I know, but couldn't think of anything else and it works for the time being :-)
Thanks for the help again.

Categorías

Más información sobre Interactive Control and Callbacks en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by