Synchronize/show variables in workspace with GUI
19 views (last 30 days)
Hopefully someone can help me.
I try to built a GUI using guide which show three variables from the workspace in a window. I am able to create the window but I have no idea how reprogram to link/synchronize the variables to the GUI. If the variable chance of value so should the value chance in the window.
Two variable (a and b) are numbers and one variable (c) is a string.
If possible could someone provide the code including the code to link the workspace variables which I can then open with guide to adjust.
That would be of great help to me.
Thanks in advance.
Geoff Hayes on 20 Dec 2014
Leon - you could add a timer to your GUI that periodically checks the workspace for the three variables, updating the GUI window (say text controls) with their (perhaps modified) values. In the _OpeningFcn of your GUI, you could create and start the timer as
% Choose default command line output for ReadFromWorkspace
handles.output = hObject;
% create the timer object
handles.timer = timer('Name','ReadFromWS', ...
% Update handles structure
% start the timer
In the above we create and start a timer that will fire every one second (period). The callback that fires is timerCallback and it takes one additional input parameter which is the handle to the GUI figure. Now we define the callback (in the GUI) as
% get the handles to the controls
handles = guidata(hFigure);
% does the variable exist in the base workspace?
% variable exists, so read it
x = evalin('base','x');
% variable doesn't exist, so set local to empty
x = ;
y = evalin('base','y');
y = ;
z = evalin('base','z');
z = '';
% update the controls
The above assumes that we are interested in reading the local variables x, y and z from the base workspace. If they exist, then we read it, and set the text controls. Note how we use the GUI figure handle to get the handles to the controls via guidata.
See the attached for an example. Try running it and creating and then modifying the base variables x, y, and z.