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

 採用された回答

Geoff Hayes
Geoff Hayes 2016 年 9 月 27 日

0 投票

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 件のコメント

Prasanna kulkarni
Prasanna kulkarni 2016 年 9 月 28 日
編集済み: Prasanna kulkarni 2016 年 9 月 28 日
sir i want show world clock .so i m showing 4 country's time here.so i am changing the time according to respective time zone. Sir will you please provide code for date adjustment in this code for different countries
Geoff Hayes
Geoff Hayes 2016 年 9 月 28 日
Prasanna - as there are only two text boxes (at least so far as your code is showing), I don't understand how you are showing 4 country's time here. Nor how you are choosing which time zones to show. Please clarify.
Prasanna kulkarni
Prasanna kulkarni 2016 年 9 月 29 日
編集済み: Prasanna kulkarni 2016 年 9 月 29 日
sir i have attached my code. please see this. In this code after execution it is showing error as 'error while evaluating TimerFcn for timer 'MyTimer'
H must be the handle to a figure or figure descendent.' what is the solution for this
Geoff Hayes
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.
Prasanna kulkarni
Prasanna kulkarni 2017 年 9 月 22 日
How to add images in front of countries in above program. Please explain with above example.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSearch Path についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by