简体   繁体   中英

Timer with MATLAB GUI is not refreshing GUI?

I had timer in my original script which worked fine and it re-ran my script every 60 seconds.

I've now moved the same script into a GUI using GUIDE and i've literally pasted it into the GUI code which GUIDE provides and it works fine. But when I include the timer code from the original script it doesn't work and it displays this error whenever I run it:

??? Error using ==> axes
Invalid object handle

Error in ==>
SoftwareMonitoringToolGUI>SoftwareMonitoringToolGUI_OpeningFcn at 100
axes(handles.axes5);

Error in ==> gui_mainfcn at 221
    feval(gui_State.gui_OpeningFcn, gui_hFigure, [],
    guidata(gui_hFigure), varargin{:});

Error in ==> SoftwareMonitoringToolGUI at 51
    gui_mainfcn(gui_State, varargin{:});

My GUI looks like this and it stays the same regardless of whether or not the timer code is included in the script ie MATLAB still builds it. The difference is that MATLAB provides the above error when I include the timer code:

在此处输入图片说明 When I simply remove the timer code only, it works again (but without the refresh feature I need using the timer). My handles are correct because MATLAB is generating my figures in the GUI window.

Timer code, placed at the start of the GUI code:

function SoftwareMonitoringToolGUI_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;



guidata(hObject, handles);

%                  ***TIMER FUNCTION***
%**************************************************************************
% 
Period = 60; % Update period in seconds

tim = timer('Period', Period, 'ExecutionMode', 'fixedRate',...
    'TimerFcn', 'SoftwareMonitoringToolGUI');

start(tim)

stop(tim) 

I want the timer to refresh the GUI every 60 seconds. The reason for this is that new data is calculated every time my script is executed. Thoughts?

When you pass a string to timer as the callback, MATLAB calls that string as executable code. So, what you are doing here:

tim = timer('Period', Period, 'ExecutionMode', 'fixedRate',...
'TimerFcn', 'SoftwareMonitoringToolGUI');

is equivalent to calling

exec('SoftwareMonitoringToolGui');

or alternatively

SoftwareMonitoringToolGui;

with no arguments. You haven't provided enough code to know exactly what's going on, but I'm guessing that SoftwareMonitoringToolGui is the name of your gui function. This means that your gui attempting to spawning a different instance of the same code, but without passing arguments. If this isn't how it is designed to work, you have problems.

What you probably want to do instead is create an update function (additional function in the same file as the gui code), and pass a handle to that function to the timer:

tim = timer('Period', Period, 'ExecutionMode', 'fixedRate',...
'TimerFcn', @updateFcn);

I think, you need to pass handles array as a param to your function in order to update your GUI controls. To do so, create timer in the following manner:

tim = timer('Period', Period, 'ExecutionMode', 'fixedRate',...
'TimerFcn', {@SoftwareMonitoringToolGUI, handles});

To make such timer work, you need to update SoftwareMonitoringToolGUI signature like this:

function SoftwareMonitoringToolGUI(obj,event,handles)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM