Danh mục

How do global variables differ from regular (local) variables?

Số trang: 31      Loại file: doc      Dung lượng: 507.50 KB      Lượt xem: 24      Lượt tải: 0    
Jamona

Xem trước 4 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Each function in MATLAB contains a set of variables specific to that function. Even in the same .m file, you don’t have (direct) access to variables created in other functions within the file. Global variables give you the ability to create/change a variable in one function and have that updated variable accessible elsewhere. This post will discuss two methods for handling (no pun intended) global variables, one of which is perfectly integrated into Graphical User Interfaces (GUIs).
Nội dung trích xuất từ tài liệu:
How do global variables differ from regular (local) variables?How do global variables differ from regular (local) variables?Each function in MATLAB contains a set of variables specific to that function. Even inthe same .m file, you don’t have (direct) access to variables created in other functionswithin the file. Global variables give you the ability to create/change a variable in onefunction and have that updated variable accessible elsewhere. This post will discuss twomethods for handling (no pun intended) global variables, one of which is perfectlyintegrated into Graphical User Interfaces (GUIs).METHOD 1: global VARIABLEThe first (non-GUI) way to create a global variable is to use the function ‘global’. Createthe global variables X, Y, and Z with the command:global XYZThe ‘global’ function needs to be called in each separate function (usually in thebeginning) where the variables will be called. Stylistically, the variable names areusually longer names and all in CAPS to indicate global variables within the functions.The documented example in the MATLAB helps shows this pretty well:function ticglobal TICTOC %define/incorporate global variable at start of functionTICTOC = clock;function t = tocglobal TICTOC %accesses variable TICTOC (or creates it if TICTOC isundefined)if nargout < 1 elapsed_time = etime(clock, TICTOC)else t = etime(clock, TICTOC);endMany hard-core coders prefer to avoid using ‘global’ except for constants. The reasonbehind this is because it’s generally considered poor form to lock up a variable name (SeeSteve L’s comment below for another reason!). While this won’t matter for smallerprograms and functions, when the files get to be many hundreds (or thousands ormillions) of lines long, it can be very difficult to keep track of all of the global variablesand to remember to call all the necessary variables at the start of each function. The greatthing about GUIs is that they already have a built-in global structure to deal with all ofyour global variables: the handles. The handles structure is an input (and thereforeaccessible) to every function in the GUI, making it perfectly capable doing everything the‘global’ command can. In fact, you shouldn’t ever have to use ‘global’ command whendesigning a GUI because the handles structure does the job so well. GUIs and ‘global’don’t mix kids!METHOD 2: handles.variableAs you may have seen from many of the blinkdagger GUI tutorials, the handles structureis an extremely useful method to manipulate GUI boxes/buttons/tools. But the tooldata are all just stored variables that can be accessed anywhere within the GUI (akaglobal variables!). Since we don’t need to edit any ‘property’ of the handles structure(e.g. handles.static_text, ‘String’), we don’t need to use the ‘get’/’set’commands. Creating the global variable is as easy as saying:handles.x = 42;%And of course, dont forget to update your handles structure:guidata(hObject, handles);handles.x is now an independent variable and note that it has no relation to the localvariable x.x = 43;is a completely valid command in the same function that would not overwrite your globalvariable ‘handles.x’.Remember, these variables can range from constants (e.g. 12) to strings (e.g. ‘HelloWorld’) to structures, cells, and arrays of constants/strings.Hopefully you can see the usefullness of global variables and will use them (properly!) inyour coding adventures.11 Responses to “MATLAB - Global Variables” 1. on 30 Jun 2009 at 9:02 am 1Andrew Scott When I started using GUIs a couple of years ago I couldn’t get this right, so ended up storing all my global variables via setappdata(gcbf, ’string’, data). I’ve never had a problem with this, although it is more cumbersome than the handles method you describe here. I think I’ll use your method in future. 2. on 30 Jun 2009 at 9:35 am 2Steve L Zane, In the first method you said “Many hard-core coders prefer to avoid using ‘global’ except for constants. The reason behind this is because it’s generally considered poor form to lock up a variable name.” That’s one reason to avoid using global variables, but a much stronger reason to avoid using global variables is that it can lead to bugs that are very difficult and time consuming to locate, particularly in the context of a GUI. Suppose I write a function that uses a global variable x. function y = myglobalsquare global x if isempty(x) x = 5; end y = x^2; In isolation, this function works perfectly fine. I set the value of the global variable x and then call myglobalsquare, and I get back the value x^2. Now I incorporate this function into a GUI. I set it as the callback for one of my GUI’s uicontrols and have another uicontrol’s callback set the value of the global variable x. I test ...

Tài liệu được xem nhiều: