Timer implementation in GUIDE

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.

Respuestas (1)

Jan
Jan el 22 de Mzo. de 2017
Editada: Jan el 22 de Mzo. de 2017
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 comentarios

Akshay A
Akshay A el 22 de Mzo. de 2017
Hi Jan,
I have created gui using guide therefore it has created hObject.
I have added axes1 while creating gui therefore it has been included in handles.
I have found workaround for plotting data on axes and to get timer2 handle(HandleVisibility property of gui figure(timer_gui.fig) is changed from CALLBACK to ON and .m file is unmodified).
Now it is plotting continuous data on axes but when I check the state of timer in plot_data_callback, it is showing 'off' though it is plotting data on axes.
Do I need to update handles even I start timer?
eg.
if strcmp(get(timer1, 'Running'), 'off')
start(timer1);
end
...
guidata(hObject, handles)
Thank you.
Jan
Jan el 22 de Mzo. de 2017
You only need to update the handles struct after changes.
Akshay A
Akshay A el 29 de Mzo. de 2017
Hi Jan,
I am not able to see timer's handle only in timer callback function.
It is creating problem when I am updating handles in timer callback function.
I am starting timer in "start_callback" and stopping timer in "stop_callback". I am able to see timer's handle in "stop_callback".
Thank you.
Regards, Akshay
Jan
Jan el 29 de Mzo. de 2017
@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 el 29 de Mzo. de 2017
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

Iniciar sesión para comentar.

Categorías

Más información sobre Migrate GUIDE Apps en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 22 de Mzo. de 2017

Comentada:

el 29 de Mzo. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by