MATLAB Answers


Run pushbutton automatically as soon as the GUI is visible without pressing the pushbutton

Asked by Iemad Sofi on 14 Nov 2019 at 7:08
Latest activity Commented on by Iemad Sofi on 14 Nov 2019 at 7:41
I want to run my pushbutton automatically as soon as the GUI is visible. And dont have to press the pushbutton in order to start the pushbutton. I only want the pushbutton to be started once. Can i get any help? Im new to MATLAB and i have searched but cannot find my answer.
Thankyou in advance.


Sign in to comment.

1 Answer

Answer by Walter Roberson
on 14 Nov 2019 at 7:34
 Accepted Answer

In the OpenFcn callback, create a timer object configured for a single execution, and have the callback for the timer object be a call to the name of the callback for the pushbutton, passing in the handle of the pushbutton, then [], and then the handles variable. But do it like this:
handles.push_timer = timer(... various arguments ...);
guidata(hObject, handles); %store a copy of handles complete with the push_timer
handles.push_timer.TimerFcn = @(hObject, event) YourGUI_pushbutton1_Callback(handles.pushbutton1, [], handles);
That is, do not set the TimerFcn callback to refer to handles until after you have already stored the timer object in handles. The timer() function permits you to configure TimerFcn as an option at the time you create the timer, but do not do that. If you do that, then the value of handles that makes it into the callback will not include the timer object and you would risk the timer being deleted before it gets used.

  1 Comment

Sign in to comment.