User`s guide
13 Manage Application-Defined Data
UserData Property Example: Passing Data Between
Components
Thefollowingcodeisthesameasinthepriorexample,“ShareDatawith
Nested Functions” on page 13-11, but uses the
UserData property to initialize
and increment the e rror counter. It a lso 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 elsewhere on your path as
slider_gui_userdata.m.
Alternatively, click here to place
slider_gui_userdata.m in your current
folder. Run the function by typing
slider_gui_userdata at the command
line.
function slider _gui _userdata
fh = figure('Position',[250 250 350 350],...
'MenuBar','none','NumberTitle','off', ...
'Name','Sharing Data with UserDa ta');
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;
% Set edit text UserData pr operty to slider structure.
set(eth,'UserData',slider)
% ---------- ---- ----------------------- ---------------
% Set the value of the edit text component String property
% to the value of the slider.
function sli der_ callback(hObject,eventd ata)
% Get slider from edit text UserData.
slider = get(eth,'UserData');
slider.previous_val = slider.val ;
slider.val = get(hObject,'Value');
13-16