how to automatically click a pushbutton continously
15 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Geoff Hayes
2016-9-27
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 个评论
Geoff Hayes
2016-9-29
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Entering Commands 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!