data = guidata(object_handle);
The component you specify to store the data does not need to be the same component
that you use to retrieve it.
If your data is stored as a struct array or cell array, and you want to update one element
without changing the other elements, then retrieve the data and replace it with the
modified array:
data = guidata(hObject);
data.myvalue = 2;
guidata(hObject,data);
Use guidata in Apps Created Programmatically
To use guidata in a programmatic app, store the data with some initial values in the
main function. Then you can retrieve and modify the data in any callback function.
The following code is a simple example of a programmatic app that uses guidata to
share a structure containing two fields. To see how it works, copy and paste this code into
an editor and run it.
function my_slider()
hfig = figure();
guidata(hfig,struct('val',0,'diffMax',1));
slider = uicontrol('Parent', hfig,'Style','slider',...
'Units','normalized',...
'Position',[0.3 0.5 0.4 0.1],...
'Tag','slider1',...
'Callback',@slider_callback);
button = uicontrol('Parent', hfig,'Style','pushbutton',...
'Units','normalized',...
'Position',[0.4 0.3 0.2 0.1],...
'String','Display Values',...
'Callback',@button_callback);
end
function slider_callback(hObject,eventdata)
data = guidata(hObject);
data.val = hObject.Value;
data.diffMax = hObject.Max - data.val;
% For R2014a and earlier:
% data.val = get(hObject,'Value');
11 Manage Application-Defined Data