How to enable a button after x seconds in MATLAB GUI?

1 次查看(过去 30 天)
Hi,
I am recording an audio. I have a start button and a stop button in GUI. When someone clicks start, the audio starts getting recorded.
Now, I want to enable the stop button only after x seconds. Where x could be 0.5 seconds, 1 seconds or whatever. How could I know that x seconds have passed and now its time to turn on the stop button.
Basically, I don't know how to start a clock and obtain its value. Can someone please help me with it?
Thank you.

采纳的回答

Paulo Silva
Paulo Silva 2011-6-25
function testtimebutton
figure
t = timer('TimerFcn','nan;','StopFcn',@endtimer, 'startdelay',... 2,'ExecutionMode','singleshot');
pbh1 = uicontrol(gcf,'Style','pushbutton','String','Start timer',...
'Position',[10 90 60 40],...
'callback',@stimer);
pbh2 = uicontrol(gcf,'Style','pushbutton','String','Button',...
'Position',[10 30 60 40],...
'enable','off','callback','disp(''You pressed the Button'')');
function endtimer(obj,event)
set(pbh2,'enable','on')
disp('timer stopped, Button enabled')
end
function stimer(obj,event)
start(t)
disp('timer started')
set(pbh2,'enable','off')
end
end
  2 个评论
Matlab Help Seeker
Matlab Help Seeker 2011-6-25
Dear Paulo,
Actually I have a toggle button. When I click on it first time, it starts recording. The next time you click it, it will stop recording.
function togglebutton1_Callback(hObject, eventdata, handles)
button_state = get(hObject,'Value');
if button_state == get(hObject,'Max')
recorder = audiorecorder(8000,8,1);
record(recorder);
set(handles.togglebutton1,'String','Stop Recording');
handles.recorder=recorder;
guidata(hObject,handles);
elseif button_state == get(hObject,'Min')
recorder=handles.recorder;
stop(recorder);
samples = getaudiodata(recorder,'uint8');
set(handles.togglebutton1,'String','Start Recording');
end
But what I want is that when you click on it for the first time, it starts recording. Then you can't press the button for 2 seconds. After 2 seconds, you can press the button to stop recording. This will make my recording duration of atleast 2 seconds. Also, the recording could be greater than 2 seconds but the recording shouldn't be less than 2 seconds.
So, what I want is when you click on the togglebutton, it starts recording. Then the togglebutton gets disabled (enabled=off) for 2 seconds. After atleast 2 seconds or more than 2 seconds recording, you can press the togglebutton again to stop recording.
Paulo Silva
Paulo Silva 2011-6-25
%code to start recording and start record
set(hObject,'enable','off')
pause(2)
set(hObject,'enable','on')
%now the user can click again on the button

请先登录,再进行评论。

更多回答(1 个)

Fangjun Jiang
Fangjun Jiang 2011-6-25
You can use the MATLAB timer object. Follow this

Community Treasure Hunt

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

Start Hunting!

Translated by