how to automatically click a pushbutton continously

8 views (last 30 days)
for a gui project i want to click a pushbutton automatically after every 1 second...please provide code...... my code is
t=clock;
c=fix(t);
v=c(1,4);
b=c(1,5);
set(handles.text2,'string',[v,b]);
set(handles.text3,'string',[v,b+30]);
now how to run set function after every 1 second without clicking pushbutton manually

Accepted Answer

Geoff Hayes
Geoff Hayes on 27 Sep 2016
Prasanna - if you want to periodically perform an action, then use a timer. In the OpeningFcn of your GUI, you would create the timer, add the handle to the timer to the handles structure, and then start the timer as
% create the timer
handles.timer = timer('Name','MyTimer', ...
'Period',1, ...
'StartDelay',0, ...
'TasksToExecute',inf, ...
'ExecutionMode','fixedSpacing', ...
'TimerFcn',{@timerCallback,handles.figure1});
% Update handles structure
guidata(hObject, handles);
start(handles.timer);
Note that the above timer has a period of one second and that every second the timerCallback function will fire. The body for this callback would be
function timerCallback(hObject,event,hFigure)
handles = guidata(hFigure);
if ~isempty(handles)
t=clock;
c=fix(t);
v=c(1,4);
b=c(1,5);
set(handles.text1,'String',sprintf('%d:%d:%d',v,b, c(1,6)));
set(handles.text2,'String',sprintf('%d:%d:%d',v,b, c(1,6)));
end
The third input to this function is the handle to the figure/GUI (which is set when the timer callback is initialized). We use it to get the handles structure so that we have access to the text fields that we wish to update. It wasn't clear to me what v and b are (or why you add 30 to the hour (?)), so I just used sprintf to create a string with the current time as HH:MM:SS.
When the user closes the GUI, the CloseRequestFcn is called and it stops the timer.
See the attached for an example (created with R2014a).
  5 Comments
Geoff Hayes
Geoff Hayes on 29 Sep 2016
Prasanna - if I run your code from the command line as
>> PRA
I do not observe any errors. However, if I double-click the PRA.fig file then I see the same error message as your. This is expected. GUIDE created GUIs can only be opened/launched from the command line (like I did above) or by pressing the run button in either the GUIDE editor or m-file editor. You cannot launch a GUI by double-clicking on the fig file.
Prasanna kulkarni
Prasanna kulkarni on 22 Sep 2017
How to add images in front of countries in above program. Please explain with above example.

Sign in to comment.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!