Timer implementation in GUIDE

조회 수: 19 (최근 30일)
Akshay A
Akshay A 2017년 3월 22일
댓글: Akshay A 2017년 3월 29일
Hi,
I want to implement a timer which will plot continuously data on the gui. There are two push buttons, start and stop. "start" will start the ploting on axes. "stop" will stop the plotting on axes.
*Please find the attached files.
I am facing two issues while implementing it.
1) Initially I created a timer object and added it in handles structure(handles.timer2) and updated guidata. But when I call my Timer callback function (plot_data_callback), the handle of the timer is not preset in that timer callback function.
I created another timer object(timer1) and made it global. Now Global timer object is present in the Timer callback function (plot_data_callback).
I want to use timer2. can someone point out the error in declaring timer2? (I have uploaded guide files)
2) When Start is pressed, instead of plotting on axes it is creating another figure. I have declared the current axes for the plotting but it is not working.
Thank you in advance.

답변 (1개)

Jan
Jan 2017년 3월 22일
편집: Jan 2017년 3월 22일
Avoid global variables. They cause troubles in general.
If you change the handles struct, care for writing the changes back to the figure. In the next callback, the handles struct from the input does not contain the last changes, so retrieve it directly from the figure again:
function callbackXYZ(hObject, EventData, handles_FromInput)
handles = guidata(hObject);
handles.XYZ = rand;
...
guidata(hObject, handles);
Then the handles struct is kept up-to-date between the different callbacks.
The handles of the timer is the first input of the TimerFcn. You do not need a global to get it.
Where is "handles.axes1" created? Perhaps you should create it in the OpeningFcn:
function timer_gui_OpeningFcn(...)
handles.axes1 = axes('Parent', hObject, 'NextPlot', 'add');
handles.hFigure = hObject; % Is this existing already?
guidata(hObject, handles);
Note that with "'Timerfcn',{@plot_data_callback,handles}" you define the 3rd input of the TimerFcn with the static value of the handles struct. If you want to access the current version stored in the figure, you have to retrieve it again in the timer callback:
function plot_data_callback(hObject, eventdata, handles_FromInputs)
hFigure = handles_FromInputs.hFigure;
handles = guidata(hFigure); % Current value
...
Then you should have access to the axes created inside the figure also.
  댓글 수: 5
Jan
Jan 2017년 3월 29일
@Akshay: I do not understand the sentence "I am not able to see timer's handle only in timer callback function". What does this mean? Please post the code and the error message.
Akshay A
Akshay A 2017년 3월 29일
Hi Jan,
Let me rephrase the sentence. While debugging, handle of the timer is not visible in the timer callback function.
function plot_data_callback(hObject, eventdata,handles)
% global t;
% guidata(hObject)
% global timer
% handles = guidata(hfigure);
x = rand(1,10);
y = rand(1,10);
axes(handles.axes1)
plot(gca,x,y);
timer_status = get(handles.timer2,'Running')
In the above code, I am trying to check the status of timer2. It is showing "OFF" in the timer callback function but if I check the timer status in stop_callback, it is showing "ON". I am not able to access the timer's handle in timer callback function. I have attached the recent file. Hope, now its clear.
Thank you. Regards, Akshay

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by