User`s guide
Share Data Among a GUI’s Callbacks
Both callbacks use the getappdat a and s etappdata functions to retrieve and
save the
slider_data structure as sl ider application data.
Share Data with GUI Data
GUI data, which you manage with the guidata function, is accessible to all
callbacks of the GUI. A callback for one component can set a value in GUI
data, w hich can then be read by a callback for another component. For more
information, see “GUI Data” on page 13-8.
GUI Data Example: Passing Data Between Components
The following code is similar to the code of the previous topic, but uses GUI
data to initialize and maintain the old and new slider values in the edit text
and slider callbacks. It also uses nested functions to provide callbacks with
access to other component’s handles, which the main function defines. Copy
the following code listing, paste it into a new file, and save it in your current
folder or elsewh ere on your path as
slider_gui_guidata.m.Alternatively,
clickheretoplace
slider_gui_guidata.m in your current folder. Run the
function by typing
slider_gui_guidata at the command line.
function slider _gui _guidata
fh = figure('Position',[250 250 350 350],...
'MenuBar','none','NumberTitle','off', ...
'Name','Sharing Data with GUI Data');
sh = uicontrol(fh,'Style','slid er',...
'Max',100,'Min',0,'Value',25,...
'SliderStep',[0.05 0.2],...
'Position',[300 25 20 300],...
'Callback',@slider_callback);
eth = uicontrol(fh,'Style','edi t',...
'String',num2str(get(sh,'Value')),...
'Position',[30 175 240 20],...
'Callback',@edittext_callback);
sth = uicontrol(fh,'Style','tex t','String',...
'Enter a value or click the slider.',...
'Position',[30 215 240 20]);
number_errors = 0;
slider.val = 25;
guidata(fh,slider);
13-21