Synchronize/show variables in workspace with GUI

7 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.

Accepted Answer

Geoff Hayes
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', ...
'Period',1, ...
'StartDelay',1, ...
'TasksToExecute',inf, ...
'ExecutionMode','fixedSpacing', ...
% Update handles structure
guidata(hObject, handles);
% 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
function timerCallback(~,~,hFigure)
% get the handles to the controls
handles = guidata(hFigure);
% does the variable exist in the base workspace?
if evalin('base','exist(''x'',''var'')')
% variable exists, so read it
x = evalin('base','x');
% variable doesn't exist, so set local to empty
x = [];
if evalin('base','exist(''y'',''var'')')
y = evalin('base','y');
y = [];
if evalin('base','exist(''z'',''var'')')
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.
Dipesh  Mudatkar
Dipesh Mudatkar on 2 Jul 2018
Hi Geoff Hayes,
I am getting following error while running the GUI.
Error while evaluating TimerFcn for timer 'ReadFromWS'
H must be the handle to a figure or figure descendent.
Geoff Hayes
Geoff Hayes on 3 Jul 2018
Dipesh - you will need to show the code that you have written (or adapted) that is leading to this error message.

Sign in to comment.

More Answers (0)


Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!