Synchronize/show variables in workspace with GUI
9 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Leon
am 16 Dez. 2014
Kommentiert: Geoff Hayes
am 3 Jul. 2018
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.
0 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 20 Dez. 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 Kommentare
Dipesh Mudatkar
am 2 Jul. 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
am 3 Jul. 2018
Dipesh - you will need to show the code that you have written (or adapted) that is leading to this error message.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Environment and Settings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!