Graphics Programs Reference
In-Depth Information
handles into a vector when they are defined. That is, we want to modify
the code as follows:
function exradio2(action)
if nargin = = 0
clf
h1 = uicontrol('Position',[200 321 90 25], ...
'String','JJJ', ...
'Style','radiobutton',...
'CallBack','exradio2(1)');
:
:
h4 = uicontrol('Position',[200 246 90 25], ...
'String','5AD', ...
'Style','radiobutton',...
'CallBack','exradio2(1)');
% Save radio button handles in h for later use.
h = [h1 h2 h3h4];
else
ind = find(h~ = gco);
set(h(ind),'value',0);
end
This implementation will not work because function variables are local to
the function and do not persist from one function call to another. When
a radio button is pushed it will issue a callback to exradio2 , which will
go to the else section of code where it will crash because the variable
h will not be defined. One way to implement the idea correctly is to
declare the vector h to be global . Global variables are visible to all other
functions that declare them global, and thus they will be visible between
one function call and the next. The correct implementation will be
function exradio2(action)
global h
if nargin = = 0
clf
:
:
You can even access such global variables from the matlab workspace
(the command window) by declaring them global there.
Variables in UserData
A problem with global variables is that they are vulnerable to being
cleared by the user from the workspace. If the user clears the global
variables that a function expects to be present, then the function will
Search WWH ::




Custom Search