12.07.2015 Views

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

Basics of MATLAB and Beyond

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

h<strong>and</strong>les into a vector when they are defined. That is, we want to modifythe code as follows:function exradio2(action)if nargin = = 0clfh1 = 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 h<strong>and</strong>les in h for later use.h = [h1 h2 h3h4];elseind = find(h~ = gco);set(h(ind),’value’,0);endThis implementation will not work because function variables are local tothe function <strong>and</strong> do not persist from one function call to another. Whena radio button is pushed it will issue a callback to exradio2, which willgo to the else section <strong>of</strong> code where it will crash because the variableh will not be defined. One way to implement the idea correctly is todeclare the vector h to be global. Global variables are visible to all otherfunctions that declare them global, <strong>and</strong> thus they will be visible betweenone function call <strong>and</strong> the next. The correct implementation will befunction exradio2(action)global hif nargin = = 0clf::You can even access such global variables from the matlab workspace(the comm<strong>and</strong> window) by declaring them global there.Variables in UserDataA problem with global variables is that they are vulnerable to beingcleared by the user from the workspace. If the user clears the globalvariables that a function expects to be present, then the function willc○ 2000 by CRC Press LLC

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!