12 Manage Application-Defined Data
'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;
guidata(hObject,data);
end
function button_callback(hObject,eventdata)
data = guidata(hObject);
display([data.val data.diffMax]);
end
When the user moves the slider, the slider_callback function calls guidata to
retrieve a copy of the stored struct array. The next two lines of code modify the struct
array. The last line in the function replaces the stored struct array with the modified
copy.
When the end users clicks the push button, the button_callback function calls
guidata to retrieve a copy of the stored struct array. Then it displays the two values
stored in the array.
Use guidata in a GUIDE UI
GUIDE uses guidata to store a struct array called handles, which contains all the
UI components. MATLAB passes the handles array to every callback function. If you
want to use guidata to share additional data, then add fields to the handles structure
in the OpeningFcn callback. To modify your data in a callback function, modify the
handles array, and then store it using guidata. This slider callback function shows
how to modify and store the handles array in a callback function.
function slider1_Callback(hObject,eventdata,handles)
handles.myvalue = 2;
guidata(hObject,handles);
end
To see a full example of a GUIDE UI that uses the guidata function, follow these steps
to copy and examine the code.
1 Set your current folder to one to which you have write access.