1. Trang chủ
  2. » Công Nghệ Thông Tin

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

31 257 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 31
Dung lượng 507,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

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

Trang 1

How do global variables differ from regular (local)

variables?

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)

METHOD 1: global VARIABLE

The first (non-GUI) way to create a global variable is to use the function ‘global’ Create the global variables X, Y, and Z with the command:

global X Y Z

The ‘global’ function needs to be called in each separate function (usually in the

beginning) where the variables will be called Stylistically, the variable names are usually longer names and all in CAPS to indicate global variables within the functions The documented example in the MATLAB helps shows this pretty well:

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 (See Steve L’s comment below for another reason!) While this won’t matter for smaller programs and functions, when the files get to be many hundreds (or thousands or

millions) of lines long, it can be very difficult to keep track of all of the global variables and to remember to call all the necessary variables at the start of each function The great thing about GUIs is that they already have a built-in global structure to deal with all of your global variables: the handles The handles structure is an input (and therefore

accessible) 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 when

Trang 2

designing a GUI because the handles structure does the job so well GUIs and ‘global’ don’t mix kids!

METHOD 2: handles.variable

As you may have seen from many of the blinkdagger GUI tutorials, the handles structure

is an extremely useful method to manipulate GUI boxes/buttons/tools But the tool data are all just stored variables that can be accessed anywhere within the GUI (aka global 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:

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 2 Steve L

Zane,

Trang 3

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

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 it and it works, and I go on finishing up the GUI But suddenly, when I introduce a new object into the GUI and set up its callback, my “Square” button

no longer works! What’s going on? I haven’t made any changes to the Square button in hours!

The problem, I find out after spending a LOT of time looking at myglobalsquare and the callback that sets the global variable x, is that the callback for the new object I introduced in the GUI _also_ uses the global variable x Because of that callback function’s manipulation of the global variable, the myglobalsquare function no longer works the way I expect it to

If you think of a function workspace as a house, with the input arguments coming

in the front door and the output arguments leaving by the back door, a global variable is an open window that anyone can crawl through and move your

furniture around, potentially without you realizing it until you invite a very

important guest (the data for the real problem you’re trying to solve, not the data you’ve used to test the function) for a visit

On a side note, I suppose that would make a nested function an apartment in an apartment complex, where the landlord has the right to come in and move stuff around in certain circumstances [mainly maintenance or emergency situations] and the resident doesn’t have the right to do certain things [like paint the walls.]

3 on 30 Jun 2009 at 10:22 am 3 Zane Montgomery

Trang 4

I love the analogy Thanks for the tip Steve!

4 on 01 Jul 2009 at 6:43 am 4 MATLABDoug

ARGHH! Globals make me crazy…

The handles structure is not global It is passed around to all the callbacks in a GUI by default, but it is not global

I know this is partially a religious issue, but I see way too many people abusing true global variables because they do not understand scoping of variables and then they end up in a real mess later

Please do not use global variables unless you can explain to your teddy bear (http://sjbdeveloper.blogspot.com/2006/03/teddy-bear-code-reviews.html) why you really need them My bet is you would be better off without them See below for alternatives

On to my next religious issue: the handles structure is for handles It just feels untidy to put data in a structure specifically labeled for handles

I see people putting just tons of data into the handles structure and it becomes a real mess Is it going to cause as many problems as globals? Not likely However,

I highly recommend the use of GETAPPDATA and SETAPPDATA as shown in this video:

http://blogs.mathworks.com/videos/2005/10/03/guide-video-part-two/

To me, this is cleaner

Loren is a fan of nested functions

scope/

http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-Please consider the alternative before using unneeded globals Globals are often the cause of really tricky errors Globals are just quicker, easier, more seductive.Use nested functions or appdata, not as clumsy or random as a global An elegant weapon, for a more civilized age

-Doug

 Anonymous

Is it ok to use the userdata from the root?

Trang 5

Whilst I appreciate the elegance of Method 2, would switching to this effectively double

my memory usage (with one copy of the data in the GUI’s handles structure, and the other in the local handles copy)?

 on 01 Aug 2009 at 10:43 am 7Dey

Hi all

I apologize in advance because I don’t have a clue how start a new post…

I need a script which does the following.:

I have the following directory structures

I want to go through the folders levelx_y_z(where x, y, z stands for 1_1_1, 1_1_2 etc.)

to get the files *.ext I don’t want to select these files manually with ‘uigetfile’, because it’s

very time consuming Could anyone help me please I tried something with ‘fullfile(path,

‘*.ext’)’ but then I still have

to specify each path

Trang 6

P.s The folders levelx/levelx_y/level/x_y_z are named otherwise actually I just wanted to

illustrate the structure

 who can everyone help me!

i’m unstill about the gui ,i want to show the transfer function iin the Guide ,but how don’t know begin

Having a Problem trying to plot an imported array,

the gui does not except a code from another m file even though it’s shown in the

It may be a good idea to experiment a bit with regexp to get familiar with it since it can

be a bit tricky to get the expressions right

MATLAB GUI Tutorial - UITABLE Part 2, How To Access Table Data

Last time, we learned how to display data onto a table This time, we’re going to learn how to work with table data within a GUI framework For example, say you wanted to take the contents of the table manipulate the data This tutorial will explain how to do that, and much more

Trang 7

• Accessing Table Data within GUI Callbacks

• Accessing only the Selected Data

• Next Time

• Links and Downloads

Accessing Table Data within GUI Callbacks

Let’s say you have the following GUI:

Trang 8

For simplicity sake, let’s assume that you would like to create a button that will add 3 to each of the entries of the table when the button is pressed How would you go about doing this? It’s actually quite straightforward Let’s take a look at the callback for the add button:

function add_pushbutton_Callback(hObject, eventdata, handles)

% hObject handle to add_pushbutton (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

%get the table data

Trang 9

Accessing only the Selected Data

Now, let’s up the difficulty level a bit Let’s say you selected a couple of cells that you want to sum, as shown in the image below (You can hold onto the CTRL button while clicking on individual cells to select multiple cells)

How would you go about doing this? Read on and all will be revealed

Enabling CellSelectionCallback

The first thing we need to do is to enable the Cell Selection Callback But first, why are

we doing this? Enabling this callback will allow us to keep track of what cells are

Trang 10

being selected on the table You can do this by bringing up the Property Inspector for

the UITABLE, and then clicking the following icon as shown in the image below

If you did it correctly, your m-file should have been updated to include the following:

% - Executes when selected cell(s) is changed in uitable1.

function uitable1_CellSelectionCallback(hObject, eventdata, handles)

% hObject handle to uitable1 (see GCBO)

% eventdata structure with the following fields (see UITABLE)

% Indices: row and column indices of the cell(s) currently selecteds

% handles structure with handles and user data (see GUIDATA)

In addition, the CellSelectionCallback field should now be populated as shown in the image below:

Trang 11

Adding the Necessary Code

First, let’s create and initialize a variable to hold the table cell selection information We will call this handles.selectedCells, and initialize it in the opening function

% - Executes just before uitable_tutorial_02 is made visible.

function uitable_tutorial_02_OpeningFcn(hObject, eventdata, handles, varargin)

% This function has no output args, see OutputFcn.

% hObject handle to figure

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

% varargin command line arguments to uitable_tutorial_02 (see

% - Executes when selected cell(s) is changed in uitable1.

function uitable1_CellSelectionCallback(hObject, eventdata, handles)

% hObject handle to uitable1 (see GCBO)

% eventdata structure with the following fields (see UITABLE)

Trang 12

% Indices: row and column indices of the cell(s) currently

selecteds

% handles structure with handles and user data (see GUIDATA)

%every time the cell selection changes, we update this data

%eventdata stores the indices of the selected cells

handles.selectedCells = eventdata.Indices;

%update the gui data

guidata(hObject, handles);

Adding the Selected Numbers Together

First, we’re going to add another button and a static text component to display the sum The modified GUI looks like this:

Now, we need to write the callback for the button we just added:

% - Executes on button press in sumNumbers_pushbutton.

function sumNumbers_pushbutton_Callback(hObject, eventdata, handles)

% hObject handle to sumNumbers_pushbutton (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

Trang 13

%get the number of rows and columns

%loop through each selected cell and keep a running sum

%can anyone thing of a better way to do this?

Trang 14

Download Source Files

The MathWorks Documentation for UITABLE

Cool Things You can do with UITABLE

Doug’s Video on UITABLE

MATLAB GUI Tutorial - UITABLE Part 1, How to Display Data

With the release of MATLAB 2008b, you are now able to add tables to a GUI In the past, there was no easy way to display your data in tabular form With the UITABLE component, displaying your data in tabular form is easy, and most importantly, looks great!

Contents

• Adding a Table to Your GUI using GUIDE

• Displaying Data on the Table

• Adding Column and Row Labels

• Modifying your Table through the m-file

• Next Time

• Links and Downloads

Adding a Table to Your GUI using GUIDE

Trang 15

Within the GUIDE framework, you can add a table to your GUI using the following icon from the toolbar:

Here’s what the GUI will look like within GUIDE:

Displaying Data on the Table

We are going to populate the UITABLE component with data by pushing the “Populate Table” button Thus, we’re going to need to add some code to the pushbutton’s callback

In the populate_pushbutton callback, we use the following code:

% - Executes on button press in populate_pushbutton.

function populate_pushbutton_Callback(hObject, eventdata, handles)

% hObject handle to populate_pushbutton (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB

% handles structure with handles and user data (see GUIDATA)

%first, create a data matrix that has 5 columns, 4 rows

Trang 16

A neat feature is that the table is smart enough to fill out the table according to the size of the data matrix that you feed it So if I had done the following instead:

myData = rand(100,100);

The table would incorporate the use of scroll bars, as shown below

Adding Column and Row Labels

A good way to spruce up your table is to add row and column labels This helps

differentiate your data and makes it easy to identify Within the GUIDE framework, we

Trang 17

can modify the labels by first bringing up the Property Inspector for the UITABLE This can be done by double clicking the UITABLE component

Now, if you click on any of the fields in the above picture, it will bring up the Table Property Editor This is where you can add Row and Column labels For example:

Trang 18

Make sure you click on the “Rows”, and that you select the “Show names entered below

as the row headers” option Finally, you just need to modify the names Similarly, you can do the same for the columns

Trang 19

Once you’re done with that you should see the following:

Trang 20

And once you run your GUI, you can see the final result A well labeled table that displays your data beautifully!

Modifying your Table through the m-file

Trang 21

Sometimes it’s easier to work from within the m-file framework, rather than the GUIDE framework We could have done exactly what we did above programmatically through the m-file In the opening function we could have done the following:

%store the row headers into a cell array

Next Time

Next time, we’re going to talk about how to work with manipulating the data within the table

Links and Downloads

Download Source Files

The MathWorks Documentation for UITABLE

Cool Things You can do with UITABLE

Doug’s Video on UITABLE

MATLAB GUI - Tool Tips are your Friends!

What is a Tool Tip?

A tool tip is supplementary information about a GUI component that appears when the user hovers the mouse cursor over the GUI component As you can imagine, this can be quite useful for the user See below for examples

Ngày đăng: 01/08/2014, 08:20

TỪ KHÓA LIÊN QUAN

w