I have a MATLAB GUI and a separate application that writes data to a file. I'd like my MATLAB GUI to check the file periodically, and update the GUI when it changes.
In Java, I'd use a SwingUtils.Timer(sp?) object to do something like this. Does MATLAB have timer functionality? I could write a java class and do it I suppose, but want something quick and dirty for a demo, preferably pure MATLAB.
You can create timer objects in MATLAB using the TIMER function. For example, this creates a timer object which should execute the function myFcn once every 10 seconds after the timer is started:
timerObject = timer('TimerFcn',@myFcn,'ExecutionMode','fixedRate',...
'Period',10.0);
Timers are started and stopped using the functions START and STOP. You should also always remember to delete them with DELETE when you're done using them. You can find more info on using timers in the MATLAB documentation.
It is worth noting, that if you are wanting to update axes object in a GUIDE GUI, there's an additional bit of "trickery" needed to make this work. You must either change the HandleVisibility property of the axes object in GUIDE, or you must explicitly acquire the handle. To do this, change the timerObject construction as follows (This is assuming the axes window in your GUIDE generated GUI is called axes1):
timerData.axes = handles.axes1;
timerData.n = 1; % some state needed for the plots.
timerObject = timer('TimerFcn',@myFcn,...
'ExecutionMode','fixedRate',...
'Period',10.0,...
'UserData', timerData);
then in myFcn
, we need to reference the axes object. Specifically:
function [] = myFcn(timerObj, event)
timerData = get(timerObj, 'UserData');
plot(timerData.axes, (1:n)/n, sin(20*2*pi*(1:n)/n));
line( (1:n)/n, cos(20*2*pi*(1:n)/n, 'Parent', timerData.axes);
timerData.n = timerData.n + 1;
set(timerObj, 'UserData', timerData);
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With