User`s guide

Ways to Manage Data in a Programmatic GUI
See “Share Data with Nested Functions” on page 13-11 for a complete
example.
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 editte xt1_ callback(hObject,eventd ata)
mystring = get(hObject,'String' );
set(hObject,'UserData',mystring);
2 A push button retrieves the string from the edit text component UserD ata
property.
function pushbu tton 1_callback(hObject,even tdata)
string = get(edittexthandle,'Us erData');
For example, if the menu item is Undo, its code could reset the String
of edittext1 back to the value stored in its UserData . To facilita te undo
operations, the
UserData can be a cell array o f strings , managed as a stack
or circular buffer.
Specify
UserData as a structure if you want to store multiple variables. Each
field you define can hold a different variable.
Note To use hObject (the calling object’s handle), you must specify a
component’s callback properties as function handles rather than as strings
or function names. When you do, the component handle is automatically
passed to e ach callback as
hObject. See “Specify Callbacks in Function Calls”
on page 12-11 for mo re information.
13-5