Share Data Between UI Callbacks Using Alternatives to Global Variables

Learn two alternative methods to using global variables for sharing data within graphical object applications.

Avoiding the use of global variables is a general rule for many programming languages. In practice, when you declare too many variables in the global scope, the code complexity increases, and debugging is more time intensive. In addition, many GUI applications contain interdependent controls and graphical objects. Since each callback to a graphical object has its own scope, data for the scope must be shared explicitly, which is time consuming.

Two cleaner and more intuitive methods that you can use as alternatives to global variables for sharing data within graphical object applications are:

Method 1: Store data in object properties with userdata and tag funtions

UI graphical objects contain properties holding certain information for objects. For example, one you can get or set the position of the graphical object by using its value.

The following code uses the userdata and tag properties to share information between the slider and pushbutton UI components.

clc; clear all; close all;
handles.fig = figure('position',[600 400 300 300]);

function sld_callback(h,v)
       slider_val = get(h,'value');
       var1 = get(h,'max') - slider_val;
       h.userdata = [slider_val,var1];
end

function btn_callback(h,v)
       slider_h = findobj('tag','slider');
       data = get(slider_h,'userdata');
       display(['value from slider callback is ', num2str(data(2))])
end

handles.slider = uicontrol(handles.fig,'style','slider','units','norm','position',[0.25 0.65 0.5 0.15],
'callback',@sld_callback,'tag','slider',...
                            'userdata',[0,1]);

handles.ptb =uicontrol(handles.fig,'style','pushbutton','string','difference','units','norm','position',
[0.25 0.25 0.5 0.25],'callback',@btn_callback);
undock


When you move the slider, the slider callback is used in a structure.

h.userdata = [slider_val,var1]; % stores slider_val, var1 as a matrix in userdata property of slider.

When a push button is clicked, the callback uses these commands to retrieve the data:

slider_h = findobj('tag','slider');  %finds the slider component by using “tag” property
data = get(slider_h,'userdata');  %gets slider_val and var1 values using slider userdata property
undock

Method 2: Store as application data with setappdata and getappdata functions

Note: setappdata and getappdata are undocumented functions.
Setappdata is used to store data for a graphical object as
setappdata(handles.fig,'sliderval',0); 

where,

1st argument is the object name.

2nd argument is variable name to the value.

3rd argument is the value.

getappdata is used to retrieve data from an graphical object as

getappdata(h_fig,'sliderval');

where,

1st argument is the object name.

2nd argument is variable name that we want to retrieve.

The following code shows the usage of setappdata/getappdata

clc; clear all; close all;
handles.fig = figure('position',[600 400 300 300]);
setappdata(handles.fig,'sliderval',0);
setappdata(handles.fig,'val_diff',1); 
function sld_callback(h,v)
       slider_val = get(h,'value');
       h_fig = get(h,'parent');
       val_diff = get(h,'max') - slider_val;
       setappdata(h_fig,'sliderval',slider_val);
       setappdata(h_fig,'val_diff',val_diff);
end

function btn_callback(h,v)
       h_fig = get(h,'parent');
       slider_val = getappdata(h_fig,'sliderval');
       diff_val = getappdata(h_fig,'val_diff');
       disp(['[slider val,',' difference value]','=', '[',num2str(slider_val),',',num2str(diff_val),']'])
end

handles.slider = uicontrol(handles.fig,'style','slider','units','norm','position',
[0.25 0.65 0.5 0.15],'callback',@sld_callback,'tag','slider');

handles.ptb = uicontrol(handles.fig,'style','pushbutton','string','Values','units','norm','position',[0.25 0.25 0.5 0.25],'callback',@btn_callback);

undock