User`s guide
Ways to Manage Data in a GUIDE GUI
UserData Property
All GUI components, including menus and the figure itse lf have a UserData
property. Y ou can assign any valid MATLAB workspace value as the
UserData property’s value, but only one value can exist at a time. To retrieve
the data, a callback must know the handle of the component in which the
data is stored. You access
UserData using get and se t w ith the appropriate
object’s handle. T he following example illustrates this pattern:
1 An edit text component stores the user-entered string in its UserData
property:
function mygui_ edit text1(hObject, even tdat a, handles)
mystring = get(hObject,'String' );
set(hObject,'UserData',mystring);
2 A menu item retrieves the string from the edit text component’s UserData
property. The callback us es the ha ndles structure and the edit text’s Tag
property, edittext1,tospecifytheedittexthandle:
function mygui_ push button1(hObject, ev entd ata, handles)
string = get(handles.edittext1, 'UserData');
For example, if the menu item is Undo, its code could reset the String of
edittext1 backtothevaluestoredinitsUserData. To facilitate undo
operations, the
UserData can be a cell array o f strings, managed as a stack
or circular buffer.
Application Data
Application data, like UserData, is arbitrary data that is meaningful to and
defined by your application. Whether to use application data or
UserData
is a matter of choice. You attach application data to a figure or any GUI
component (other than ActiveX controls) with the functions
setappdata and
getappdata, The main differences between it and Use rData are:
• You can assign multiple values to application data, but only one value
to
UserData
• Your code must reference application data by name (like using a Tag), but
can access
UserData like any other property
9-5