Synchronize/show variables in workspace with GUI

9 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

Geoff Hayes
Geoff Hayes il 20 Dic 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', ...
'TimerFcn',{@timerCallback,handles.figure1});
% Update handles structure
guidata(hObject, handles);
% start the timer
start(handles.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');
else
% variable doesn't exist, so set local to empty
x = [];
end
if evalin('base','exist(''y'',''var'')')
y = evalin('base','y');
else
y = [];
end
if evalin('base','exist(''z'',''var'')')
z = evalin('base','z');
else
z = '';
end
% update the controls
set(handles.text1,'String',num2str(x));
set(handles.text2,'String',num2str(y));
set(handles.text3,'String',z);
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.
  2 Commenti
Dipesh  Mudatkar
Dipesh Mudatkar il 2 Lug 2018
Hi Geoff Hayes,
I am getting following error while running the GUI.
Error
Error while evaluating TimerFcn for timer 'ReadFromWS'
H must be the handle to a figure or figure descendent.
Geoff Hayes
Geoff Hayes il 3 Lug 2018
Dipesh - you will need to show the code that you have written (or adapted) that is leading to this error message.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Environment and Settings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by