MATLAB Answers

Call a Pushbutton within a timer object using guide

6 views (last 30 days)
Daemonic
Daemonic on 5 Feb 2017
Commented: Daemonic on 6 Feb 2017
Hello. I'm fairly new to using GUIDE so thank you for your patience. I have gui with "Record On/Off" pushbutton which serves to start and stop a timer object. The default setting is therefore infinite in that the timer object will run until the stop button is pressed. Additionally, when pushbutton is pressed to stop the timer, several other processes are performed (e.g., changing the color of the button, preprocessing and saving the data generated during the timer object etc.).
However, I have an optional setting in which the user can specify a custom duration. When a duration is specified, the 'TasksToExecute' is set thus stopping the timer automatically after the duration has elapsed.
The issue is, that because the Recording pushbutton executes several operations in addition to stopping the timer, I need to trigger the button press via the timer's StopFcn. Being a novice with guide, I'm confused about executing callbacks within another call back. I've pasted the relevant sections of code below:
function Record_Callback(hObject, eventdata, handles)
% hObject handle to Record (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
RecordStatus = get(hObject,'Value');
runstr=get(hObject,'string');
if RecordStatus == 1
cla; %clears current axes if applicable
set(hObject, 'string', 'STOP Recording'); %updates text of record button
set(hObject, 'ForegroundColor', 'black'); %updates color of record button
Duration_MS = handles.dur * (60 * handles.hertz); %Pre-allocates duration of recording based on sample rate and time
if Duration_MS > 0;
NumExecutions = [handles.dur * 60] / get(handles.tmr, 'Period');
set(handles.tmr, 'TasksToExecute', NumExecutions);
end
% does some other cool stuff irrelevant here
start(handles.tmr); %starts the timer function
guidata(hObject, handles);
elseif RecordStatus == 0
stop(handles.tmr); %stops the timer (and stops recording)
timeElapsed = toc;
% does some other cool stuff after the timer is stopped.
Here is the StopFcn which I'd like to figure out how to simulate a button press (i.e. stop recording).
function StopRNG_callback(obj,event,hObject)
handles=guidata(hObject);
disp(['recording should have stopped at ' toc]); %shows when recording ended.
% So here is where I need to call the Record pushbutton callback. But I don't understand how to execute a callback within another callback.
guidata(hObject, handles);

  0 Comments

Sign in to comment.

Accepted Answer

Geoff Hayes
Geoff Hayes on 6 Feb 2017
Daemonic - you should be able to call the callback as you would any other function provided that you pass in the correct parameters (in particular, the handle to the control that you wish to invoke the callback on).
But instead of doing this, why not create a function that has the code common to your two callbacks and invoke that from both? Your code would then look something like
function Record_Callback(hObject, eventdata, handles)
RecordStatus = get(hObject,'Value');
runstr=get(hObject,'string');
if RecordStatus == 1
% start recording
else
% stop recording
StopRecording(handles);
end
In your timer stop function (which perhaps could be named OnTimerStoppedCallback or something similar), you would do
function StopRNG_callback(obj,event,hObject)
% stop recording
StopRecording(guidata(hObject);
Finally, your StopRecording function would do all of the work
function StopRecording(handles)
% do stuff...
All the code to stop the recording is in one function that is called by either callback.

  1 Comment

Daemonic
Daemonic on 6 Feb 2017
Fantastic! I'm still tinkering with it, but this solution appears to be working quite well. Thank you!!

Sign in to comment.

More Answers (0)


Translated by