How to update MATLAB GUI in background?

I have a MATLAB GUI and a separate application that writes data to a file. I would like my MATLAB GUI to periodically check the file and update the GUI when it changes.

In Java, I would use the SwingUtils.Timer (sp?) Object to do something like this. Does MATLAB have a timer function? I could write a java class and do this, I suppose, but want something quick and dirty for a demonstration, preferably pure MATLAB.

+4
source share
1 answer

You can create timer objects in MATLAB using the TIMER function. For example, this creates a timer object that should execute the myFcn function once every 10 seconds after the timer starts:

timerObject = timer('TimerFcn',@myFcn,'ExecutionMode','fixedRate',... 'Period',10.0); 

Timers start and stop using the START and STOP functions. You should also always remember to delete them with DELETE when you finish using them. You can find more information about using timers in the MATLAB documentation .

It is worth noting that if you want to update the axes object in the GUIDE GUI, you will need the extra β€œcheat” bit needed to do this job. You must either change the HandleVisibility property of the axes object in GUIDE, or you must explicitly get a handle. To do this, modify the timerObject construct as follows (this assumes that the axis window in your 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 refer to the axis object. In particular:

  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 
+11
source

Source: https://habr.com/ru/post/1286122/


All Articles