execute automatically Stop button command after X seconds that Start Button is pressed - GUI

3 vues (au cours des 30 derniers jours)
Hi everybody. I have a problem with a real time demo. I am trying to execute automatically the commands into the Stop Button after X seconds that the Start Button is pressed. Here the two callback functions of the start button and stop button.
function startButton_Callback(hObject, eventdata, handles)
% hObject handle to startButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
myGUIdata=guidata(handles.realTimeSgramGUI);
% cla(handles.freq_response_plot)
handles.ttt = timer('TimerFcn',{@stopButton_Callback, handles},'StartDelay',62);
disp('Start')
audioFile='file1.wav';
[audioFileFCN, Fs_audio] = audioread(audioFile);
start(handles.ttt)
sound(audioFileFCN,Fs_audio);
myGUIdata = guidata(handles.realTimeSgramGUI);
set(myGUIdata.saveButton,'enable','off');
set(myGUIdata.startButton,'enable','off');
set(myGUIdata.playButton,'enable','off');
set(myGUIdata.stopButton,'enable','on');
set(myGUIdata.displayPopupMenu,'enable','off');
switch get(myGUIdata.timer50ms,'running')
case 'on'
stop(myGUIdata.timer50ms);
end
myGUIdata.audioRecorderCount = myGUIdata.maxAudioRecorderCount;
myGUIdata.lastPosition = 1;
record(myGUIdata.recordObj1);
switch get(myGUIdata.timer50ms,'running')
case 'off'
start(myGUIdata.timer50ms);
case 'on'
otherwise
disp('timer is bloken!');
end
guidata(handles.realTimeSgramGUI,myGUIdata);
end
function stopButton_Callback(hObject, eventdata, handles)
global durToClass
global train_net
% hObject handle to stopButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%%%%%%% CLASSIFICATION%%%%%%
%%% GET all data from when Start is pressed;
myGUIdata = guidata(handles.realTimeSgramGUI);
delete(handles.ttt)
myGUIdata.audioData = getaudiodata(myGUIdata.recordObj1);
recorded = myGUIdata.audioData(:,1)+ 1i*myGUIdata.audioData(:,2);
% duration = floor(size(recorded,1)/22000)
% Start_C = size(recorded,1)- 22000*durToClass +1
% Stop_C = size(recorded,1)
Bl = str2double(get(handles.Bl_editText,'String'));
f_r = str2double(get(handles.risonanceFrequency_editText,'String'));
q_ts = str2double(get(handles.q_ts_editText,'String'));
q_es = str2double(get(handles.q_es_editText,'String'));
coil_rad = str2double(get(handles.coilRadius_editText,'String'));
f_v1 = str2double(get(handles.fv1_editText,'String'));
f_v2 = str2double(get(handles.fv2_editText,'String'));
volt= str2double(get(handles.volt_editText,'String'));
f_s=str2double(get(handles.frequencySampling_editText,'String'));
lengthAcq=str2double(get(handles.lengthAcquisition_editText,'String'));
wavelength=str2double(get(handles.wavelength_editText,'String'));
fft_bins=str2double(get(handles.fftbins_editText,'String'));
t=linspace(0,lengthAcq,lengthAcq*f_s);
f_v=f_v1*((f_v2/f_v1)^(1/lengthAcq)).^t;
toClass = recorded(end-22000*durToClass +1:end);
toClass= toClass- mean(toClass);
toClass=toClass./max(abs(toClass));
toClass=lowpass(toClass,5000,f_s);
% whos toClass
downSamplingmic=1;
[orig_ss,Fs_audio]=audioread('file.wav');
orig_ss=orig_ss(1*Fs_audio + 1:downSamplingmic:(1 + lengthAcq)*Fs_audio,1);
envelope_sig=resample(orig_ss,f_s,Fs_audio).';
gamma_c=1./(1-(f_v.^2./f_r^2)+(1i*f_v./f_r/q_ts));
% gamma_c2=1./(1-(fv.^2./fs^2)+(1i*fv./fs/2));
displ_amplitude=volt.*gamma_c./(2*pi*f_r*Bl*q_es);
range=abs(displ_amplitude).*envelope_sig;
sigma=2*pi*coil_rad/wavelength^2;
y=sigma.*(cos(4*pi*range./wavelength)+1i*sin(4*pi*range./wavelength)); %((1./(1-xxx)).^2).*
% y=sigma.*(1./(1-xxx)).^2.*(4*pi*xxx./lambda + 1i*sin(4*pi*xxx./lambda));
y = y-mean(y);
y = y./max(abs(y));
resampled_freq=linspace(1,(2*lengthAcq)*f_s-1,4096);
[impulse_response, lag]=xcorr(angle(toClass(:)),angle(y(:)));
% [impulse_resp_phase, ~]=xcorr(toClass(:)./abs(toClass(:)),y(:)./abs(y(:)));
%
[~, pos]=max(abs(impulse_response));
time_start=lag(pos)/f_s
[im_re_full, ~]=xcorr(angle(toClass(round(time_start*f_s)+1:round((time_start+lengthAcq)*f_s))),angle(y(:)));
DUT=FR_generation(im_re_full)
axes(handles.freq_response_plot)
semilogx(linspace(0,f_s/2,length(DUT(1,:))),DUT(1,:),'b','LineWidth',1);
hold on
semilogx(linspace(0,f_s/2,length(DUT(1,:))),DUT(2,:),'r','LineWidth',1);
semilogx(linspace(0,f_s/2,length(DUT(1,:))),DUT(3,:),'k','LineWidth',1);
semilogx(linspace(0,f_s/2,length(DUT(1,:))),DUT(4,:),'g','LineWidth',1);
semilogx(linspace(0,f_s/2,length(DUT(1,:))),DUT(5,:),'m','LineWidth',1);
%%% LOAD CLASSIFIER %%%%
%%%% CLASSIFY%%%%
DUT_cell=mat2cell(DUT,5);
whos DUT_cell
YPred = classify(train_net,DUT_cell,'MiniBatchSize',1,'SequenceLength','longest');
% whos YPred
%classNumber = randi(5,1); %%% OUTPUT CLASSIFICATION
switch YPred
case categorical(1)
CurrentClass = 'Good';
ColorTD = 'green';
case categorical(2)
CurrentClass = 'Bad';
ColorTD = 'red';
case categorical(3)
CurrentClass = 'Bad2';
ColorTD = 'red';
otherwise
CurrentClass = 'Undefined';
ColorTD = 'red';
end
%%% VISUALIZE CLASSIFICATION OUTPUT
set(myGUIdata.Class,'string', CurrentClass);
set(myGUIdata.Class,'BackgroundColor', ColorTD);
%%%%
myGUIdata = guidata(handles.realTimeSgramGUI);
set(myGUIdata.saveButton,'enable','on');
set(myGUIdata.startButton,'enable','on');
set(myGUIdata.playButton,'enable','on');
set(myGUIdata.stopButton,'enable','off');
set(myGUIdata.displayPopupMenu,'enable','on');
switch get(myGUIdata.timer50ms,'running')
case 'on'
stop(myGUIdata.timer50ms)
case 'off'
otherwise
disp('timer is bloken!');
end
myGUIdata.audioData = getaudiodata(myGUIdata.recordObj1);
stop(myGUIdata.recordObj1);
guidata(handles.realTimeSgramGUI,myGUIdata);
end
Even if the code gets into the Stop Button function, I have some errors as:
Error while evaluating TimerFcn for timer 'timer-2'
Reference to non-existent field 'ttt'
How Can I fix this? Any idea?
Thanks
Ale

Réponse acceptée

Geoff Hayes
Geoff Hayes le 19 Mar 2019
Alessio - here is the code for your timer
handles.ttt = timer('TimerFcn',{@stopButton_Callback, handles},'StartDelay',62);
Note how you are passing in the handles structure as an input to this callback. This is fine but you have to remember that this is a copy of the handles structure at the time you create the timer. And so it does not have the ttt field (for the timer object) within it and so the error message makes sense. I don't think that you want to use the same function for the timer callback as you do for the stop button. You can create a helper function that both of the other two use to do the bulk of the work (updating your GUI, etc.). Else it can be very confusing when looking at the signature
function stopButton_Callback(hObject, eventdata, handles)
as the inputs will be different depending upon who calls this function. i.e. is hObject the stop button or a timer?
  2 commentaires
Alessio Izzo
Alessio Izzo le 19 Mar 2019
Hi Geoff,
first of all thanks to have replied me. I am not sure if I understood your question. Lets say I want to use the timer in the startButton_Callback just to execute the script into the stopButton_Callback function when the timer reach 62 seconds.
What do you recommend?
Ideally, when I press the start button I want to start recording, and without press any other button, I want to process the last 62 seconds of the recorded signal with the algorithm into the stopButton_Callback.
Geoff Hayes
Geoff Hayes le 19 Mar 2019
Alessio - I recommend that you have a helper function that does the common code between the stop button callback and the timer callback. I don't recommend that you use the same function (stop button callback) for both tasks because you will encounter the error from above. For example, try doing
function stopButton_Callback(hObject, eventdata, handles)
if isfield(handles, 'ttt')
stop(handles.ttt);
end
doPostProcessing(handles);
where
function doPostProcessing(handles)
% the rest of the code from your stopButton_Callback
I recommend not using global variables (is there a reason why you are doing so?). Also, I don't understand the use of
myGUIdata = guidata(handles.realTimeSgramGUI);
Is myGUIdata somehow different from the handles structure. You are doing this in both the start and stop callbacks... I'm not sure you really need to do this. Please clarify.
Then the callback for your timer would be something like
function timerCallback(hObject, eventdata, handles)
doPostProcessing(handles);
Note how both the timer callback and the stop button callback call the same helper function. (Of course, you need to assign timerCallback to your timer in the start button callback...)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Objects dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by