Here is an example using a timerFcn callback. I made a simple graphical interface with 1 axis and 1 button.
In the opening function, I initialize the chart and create a timer. In the start button callback, I start the timer and start manipulating the data. The timer function callback function simply updates the y-data of the line through its descriptor. Below are the corresponding functions from the M GUI file (cut off section init and output fcn.
function testTimer_OpeningFcn(hObject, eventdata, handles, varargin) global yx x = 0:.1:3*pi; % Make up some data and plot y = sin(x); handles.plot = plot(handles.axes1,x,y); handles.timer = timer('ExecutionMode','fixedRate',... 'Period', 0.5,... 'TimerFcn', {@GUIUpdate,handles}); handles.output = hObject; guidata(hObject, handles); % --- Executes on button press in startButton. function startButton_Callback(hObject, eventdata, handles) global yx start(handles.timer) for i =1:30 y = sin(x+i/10); pause(1) end function GUIUpdate(obj,event,handles) global y set(handles.plot,'ydata',y);
You can click the Stop button to stop the timer depending on how your GUI is structured and how / how the data is updated.
Edit: Basic Help. Some of them are quite simple, and you may already know this:
An individual object descriptor contains a bunch of properties that you can read using the get () function or the specified set () function. So, for example, maybe I wanted to change the startButton text for some reason in my GUI.
set(handles.startButton,'String','Something Other Than Start');
You may just want to set a breakpoint in your code somewhere (perhaps with the click of a button) and play with structs struct. Executing get()
commands on different objects to examine their properties.
Now the handle structure contains everything ... umm ... processes your GUI objects, as well as any custom elements that may be convenient for storing there. Most GUI callbacks automatically pass the handle structure, so you have easy access to all parts of the GUI.
Ex. The 'startButton' callback was automatically passed to handles
. Thus, I had easy access to the timer object via handles.timer
.
This leads me to embed custom stuff in handles
. In the opening function, I added a new element to the structure of the handles.timer
and handles.plot
, because I knew that they would be useful in other callbacks (for example, pressing a button and timerFcn callback).
However, to preserve these things for a long time, you need to use the "guidata" function. This function basically either saves the modified handles
structure or retrieves a copy of handles
depending on how you name it. Thus, the next line in the opening function stores the modified handle structure (added .timer and .plot) in the main graphical interface.
guidata(hObject,handles);
Basically, when you add something to handles
, you should have this line to make the change permanent.
Now another way to call it:
handles = guidata(hObject); %hObject can be any handle who is a child of the main GUI.
This will restore the structure of the handles for the graphical interface.
And the last handles.output = hObject
is only the default output when starting your GUI. If you name your GUI using the Matlab command line, like this h = myGUI;
, it should return the handle to your GUI.