Page 154 - Basics of MATLAB and Beyond
P. 154

function exradio(action)
                               if nargin =   = 0
                                 clf
                                 uicontrol(’Position’,[200 321 90 25], ...
                                     ’String’,’JJJ’, ...
                                     ’Style’,’radiobutton’,...
                                     ’CallBack’,’exradio(1)’)
                                 uicontrol(’Position’,[200 296 90 25], ...
                                     ’String’,’ABC-FM’, ...
                                     ’Style’,’radiobutton’,...
                                     ’CallBack’,’exradio(1)’)
                                 uicontrol(’Position’,[200 271 90 25], ...
                                     ’String’,’SAFM’, ...
                                     ’Style’,’radiobutton’,...
                                     ’CallBack’,’exradio(1)’)
                                 uicontrol(’Position’,[200 246 90 25], ...
                                     ’String’,’5AD’, ...
                                     ’Style’,’radiobutton’,...
                                     ’CallBack’,’exradio(1)’)
                               else
                                 h = findobj(’style’,’radiobutton’);
                                 ind = find(h~ = gco);
                                 set(h(ind),’value’,0)
                               end
                               Calling the function exradio with no arguments draws the GUI and sets
                               up the callbacks. The callbacks are identical for all the buttons: they
                               simply call exradio with a single input argument. When any button
                               is clicked, the else code is executed: it first finds all the radiobuttons
                               and returns their handles in the vector h. Then a vector, ind, of the
                               elements of h that are not equal to the Current Object (got by the call
                               gco) is defined. The radio button that has just been clicked on will
                               be the Current Object. If a radio button is pushed, its “value” toggles
                               between zero and one. The set command then sets the “value” property
                               of all the other radio buttons to zero.

                               34.4   Variables in GUIs

                               Globals
                               Variables in the matlab workspace are not visible to function m-files. If
                               you use a function m-file to implement a GUI, you often need to access
                               variables that won’t be visible to the function unless you make them
                               so. To explain this, consider the example of the exclusive radio buttons
                               given in the last section. Suppose we want to get rid of the findobj
                               command in the else section of the code by putting the radio buttons’




                               c   2000 by CRC Press LLC
   149   150   151   152   153   154   155   156   157   158   159