Top Banner

of 31

Hoc Matlab1 5691

Apr 05, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 7/31/2019 Hoc Matlab1 5691

    1/31

    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 dont have (direct) access to variables created in other functions withinthe 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 methodsfor 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. Thedocumented example in the MATLAB helps shows this pretty well:

    function ticglobal TICTOC %define/incorporate global variable at start of functionTICTOC = clock;

    function t = toc

    global TICTOC %accesses variable TICTOC (or creates it if TICTOC isundefined)if nargout < 1

    elapsed_time = etime(clock, TICTOC)else

    t = etime(clock, TICTOC);end

    Many hard-core coders prefer to avoidusing global except for constants. The reason

    behind this is because its generally considered poor form to lock up a variable name (See

    Steve Ls comment below for another reason!). While this wont 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 yourglobal 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 shouldnt everhave to use global command when designing aGUI because the handles structure does the job so well. GUIs and global dont mix kids!

  • 7/31/2019 Hoc Matlab1 5691

    2/31

    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 areall just stored variables that can be accessed anywhere within the GUI (aka global

    variables!). Since we dont need to edit any property of the handles structure (e.g.

    handles.static_text, String), we dont need to use the get/set commands. Creating theglobal variable is as easy as saying:

    handles.x = 42;%And of course, don't 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 local

    variable 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. Hello

    World) 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 couldnt get this right, so ended

    up storing all my global variables via setappdata(gcbf, string, data). Ive neverhad a problem with this, although it is more cumbersome than the handles method

    you describe here.

    I think Ill 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 its generally considered

    http://blinkdagger.com/matlab/matlab-global-variables/#comment-5616http://blinkdagger.com/matlab/matlab-global-variables/#comment-5617http://blinkdagger.com/matlab/matlab-global-variables/#comment-5617http://www.mathworks.com/http://www.mathworks.com/http://blinkdagger.com/matlab/matlab-global-variables/#comment-5617http://www.mathworks.com/http://blinkdagger.com/matlab/matlab-global-variables/#comment-5616
  • 7/31/2019 Hoc Matlab1 5691

    3/31

    poor form to lock up a variable name. Thats 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 thecontext of a GUI.

    Suppose I write a function that uses a global variable x.

    function y = myglobalsquareglobal xif isempty(x)

    x = 5;endy = 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 myGUIs uicontrols and have another uicontrols 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 nolonger works! Whats going on? I havent made any changes to the Square button

    in hours!

    The problem, I find out after spending a LOT of time looking at myglobalsquareand 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 thatcallback functions 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 youre trying to solve, not the data youve used to test

    the function) for a visit.

    On a side note, I suppose that would make a nested function an apartment in anapartment 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 doesnt have the right to do certain things [like paint the walls.]

    3. on 30 Jun 2009 at 10:22 am3Zane Montgomery

    I love the analogy. Thanks for the tip Steve!

    http://blinkdagger.com/matlab/matlab-global-variables/#comment-5618http://blinkdagger.com/matlab/matlab-global-variables/#comment-5618http://blinkdagger.com/matlab/matlab-global-variables/#comment-5618http://www.blinkdagger.com/http://www.blinkdagger.com/http://blinkdagger.com/matlab/matlab-global-variables/#comment-5618http://www.blinkdagger.com/
  • 7/31/2019 Hoc Matlab1 5691

    4/31

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

    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 feelsuntidy 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 thisvideo:

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

    To me, this is cleaner.

    Loren is a fan of nested functions

    http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/

    Please consider the alternative before using unneeded globals. Globals are often thecause 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?

    like

    http://blinkdagger.com/matlab/matlab-global-variables/#comment-5624http://blinkdagger.com/matlab/matlab-global-variables/#comment-5624http://blogs.mathworks.com/videoshttp://blogs.mathworks.com/videoshttp://blogs.mathworks.com/videos/2005/10/03/guide-video-part-two/http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/http://blinkdagger.com/matlab/matlab-global-variables/#comment-5624http://blogs.mathworks.com/videoshttp://blogs.mathworks.com/videos/2005/10/03/guide-video-part-two/http://blogs.mathworks.com/loren/2008/01/16/nested-functions-and-variable-scope/
  • 7/31/2019 Hoc Matlab1 5691

    5/31

    ud=get(0,userdata);

    ud.x=42;

    set(0,userdata,ud);

    on 02 Jul 2009 at 2:38 pm 6Anonymous

    Im writing GUIs that contain some pretty large data arrays, and dont want these to beunnecessary duplicated, using up RAM. Im currently using global variables.

    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 GUIs 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 dont have a clue how start a new post

    I need a script which does the following.:

    I have the following directory structures

    C:/Big/ecm/sDDP/new/level1/level1_1/level1_1_1/*.extC:/Big/ecm/sDDP/new/level1/level1_1/level1_1_2/*.ext

    C:/Big/ecm/sDDP/new/level1/level1_1/level1_1_3/*.ext

    ..

    .

    C:/Big/ecm/sDDP/new/level2/level2_1/level2_1_1/*.extC:/Big/ecm/sDDP/new/level2/level2_1/level2_1_2/*.ext

    C:/Big/ecm/sDDP/new/level2/level2_1/level2_1_3/*.ext

    .

    .

    .

    etc.

    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 dont want to select these files manually with uigetfile, because itsvery time consuming. Could anyone help me please. I tried something with fullfile(path,

    *.ext) but then I still have

    to specify each path.

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

    illustrate the structure

    http://blinkdagger.com/matlab/matlab-global-variables/#comment-5633http://blinkdagger.com/matlab/matlab-global-variables/#comment-5882http://blinkdagger.com/matlab/matlab-global-variables/#comment-5633http://blinkdagger.com/matlab/matlab-global-variables/#comment-5882
  • 7/31/2019 Hoc Matlab1 5691

    6/31

    who can everyone help me!

    im unstill about the gui ,i want to show the transfer function iin the Guide ,but how dontknow begin

    my following topic:for G(S)=(7s^2+16s+10)/(s^4+5s^3+11s^2+16s+10)

    and have to show :+ time set

    + bulk acceleration

    +error setin the Guide

    i hope you can make it help me

    thank you

    on 23 Jan 2011 at 11:45 am9Ameya

    Having a Problem trying to plot an imported array,

    the gui does not except a code from another .m file even though its shown in the

    workspaceKindly help

    Dey

    You can solve your problem in one line with a regular expression: regexp(text, expression)

    You can get the text input from dir and the expression below should match your files:C:/Big/ecm/sDDP/new/level\d+/level\d+_\d+/level\d+_\d+_\d+/.*\.ext

    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, were going to learn how

    to work with table data within a GUI framework. For example, say you wanted to take thecontents of the table manipulate the data. This tutorial will explain how to do that, and

    much more.

    http://blinkdagger.com/matlab/matlab-global-variables/#comment-7316http://blinkdagger.com/matlab/matlab-global-variables/#comment-7316http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-2-how-to-access-table-data/http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-2-how-to-access-table-data/http://blinkdagger.com/matlab/matlab-global-variables/#comment-7316http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-2-how-to-access-table-data/http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-2-how-to-access-table-data/
  • 7/31/2019 Hoc Matlab1 5691

    7/31

    Contents

    Accessing Table Data within GUI Callbacks

    Accessing only the Selected Data

    Next Time

    Links and Downloads

    Accessing Table Data within GUI Callbacks

    Lets say you have the following GUI:

    http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-2-how-to-access-table-data/#1http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-2-how-to-access-table-data/#2http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-2-how-to-access-table-data/#4http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-2-how-to-access-table-data/#5http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-2-how-to-access-table-data/#1http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-2-how-to-access-table-data/#2http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-2-how-to-access-table-data/#4http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-2-how-to-access-table-data/#5
  • 7/31/2019 Hoc Matlab1 5691

    8/31

    For simplicity sake, lets 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 doingthis? Its actually quite straightforward. Lets 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 datatableData = get(handles.uitable1,'data');

    %add 3 to the tabletableData = tableData + 3;

    %update the tableset(handles.uitable1,'data',tableData);

    So now, when I press the Add 3 button, it adds 3 to the table! This is just a simple

    example to show how to extract the data from the UITABLE, and to perform an operation

    on it.

  • 7/31/2019 Hoc Matlab1 5691

    9/31

    Accessing only the Selected Data

    Now, lets up the difficulty level a bit. Lets say you selected a couple of cells that youwant 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 wedoing this? Enabling this callback will allow us to keep track of what cells are being

  • 7/31/2019 Hoc Matlab1 5691

    10/31

    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) currentlyselecteds% handles structure with handles and user data (see GUIDATA)

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

    image below:

  • 7/31/2019 Hoc Matlab1 5691

    11/31

    Adding the Necessary Code

    First, lets 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 (seeVARARGIN)

    %initialize this variablehandles.selectedCells = [];

    Next, we go to the uitable1_CellSelectionCallback, which is the callback that we just

    enabled.

    % --- 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)

  • 7/31/2019 Hoc Matlab1 5691

    12/31

  • 7/31/2019 Hoc Matlab1 5691

    13/31

    %get the number of rows and columns[rows,columns] = size(handles.selectedCells);

    %get the data from the UITABLEtableData = get(handles.uitable1,'data');

    %initialize the sumsum = 0;

    %loop through each selected cell and keep a running sum%can anyone thing of a better way to do this?for x=1:rows

    sum = sum +tableData(tableIndices(x,1),tableIndices(x,2));end

    %display the sum on the GUIset(handles.sum_text,'String',num2str(sum))

    And there you have it, now you can select any number of cells, and then sum up the value

    of the contents!

    Next Time

    Next time, were going to talk about some of the cool features of the UITABLE that we

    have not yet discussed, including different data types within the UITABLE.

    Links and Downloads

  • 7/31/2019 Hoc Matlab1 5691

    14/31

    Download Source Files

    The MathWorks Documentation for UITABLE

    Cool Things You can do with UITABLEDougs 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

    http://blinkdagger.com/tutorials/matlab/GUI/basic/uitable/part02/uitable_tutorial_02.ziphttp://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/uitable.htmlhttp://blogs.mathworks.com/desktop/2008/06/02/tables-in-matlab-with-uitable/http://blogs.mathworks.com/videos/2009/01/29/exploring-uitable-properties/http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-1-how-to-display-data/http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-1-how-to-display-data/http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-1-how-to-display-data/#1http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-1-how-to-display-data/#2http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-1-how-to-display-data/#3http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-1-how-to-display-data/#4http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-1-how-to-display-data/#5http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-1-how-to-display-data/#6http://blinkdagger.com/tutorials/matlab/GUI/basic/uitable/part02/uitable_tutorial_02.ziphttp://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/uitable.htmlhttp://blogs.mathworks.com/desktop/2008/06/02/tables-in-matlab-with-uitable/http://blogs.mathworks.com/videos/2009/01/29/exploring-uitable-properties/http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-1-how-to-display-data/http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-1-how-to-display-data/http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-1-how-to-display-data/#1http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-1-how-to-display-data/#2http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-1-how-to-display-data/#3http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-1-how-to-display-data/#4http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-1-how-to-display-data/#5http://blinkdagger.com/matlab/matlab-gui-tutorial-uitable-part-1-how-to-display-data/#6
  • 7/31/2019 Hoc Matlab1 5691

    15/31

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

    from the toolbar: .

    Heres 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 PopulateTable button. Thus, were going to need to add some code to the pushbuttons 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 rowsmyData = rand(5,4);

    %now populate the table with the above valuesset(handles.uitable1,'data',myData );

    Now, lets run the GUI and push the button!

  • 7/31/2019 Hoc Matlab1 5691

    16/31

    A neat feature is that the table is smart enough to fill out the table according to the size ofthe 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 can

  • 7/31/2019 Hoc Matlab1 5691

    17/31

    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:

  • 7/31/2019 Hoc Matlab1 5691

    18/31

    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.

  • 7/31/2019 Hoc Matlab1 5691

    19/31

    Once youre done with that. you should see the following:

  • 7/31/2019 Hoc Matlab1 5691

    20/31

    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

  • 7/31/2019 Hoc Matlab1 5691

    21/31

    Sometimes its 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 arrayrowHeaders = {'Blink','Dagger','Loves','MATLAB','!!!!!!'};

    %set the row labelsset(handles.uitable1,'RowName',rowHeaders);

    %do the same for the column headerscolumnHeaders = {'Quan','Daniel','Rob','Zane'};set(handles.uitable1,'ColumnName',columnHeaders);

    In this example, we assumed that we knew the dimensions of our table. If you dont knowthe size of your table beforehand, then it can be difficult to apply data labels that are

    meaningful. By working through the m-file, you obtain more flexibility since you wont

    have to go back and modify the .fig file every time you want to make a change. And if you

    are going to apply dynamic labeling, then working from the m-file is going to be mucheasier.

    Next Time

    Next time, were going to talk about how to work with manipulating the data within the

    table.

    Links and Downloads

    Download Source FilesThe MathWorks Documentation for UITABLECool Things You can do with UITABLE

    Dougs 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.

    http://blinkdagger.com/tutorials/matlab/GUI/basic/uitable/part01/uitable_tutorial_01.ziphttp://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/uitable.htmlhttp://blogs.mathworks.com/desktop/2008/06/02/tables-in-matlab-with-uitable/http://blogs.mathworks.com/videos/2009/01/29/exploring-uitable-properties/http://blinkdagger.com/matlab/matlab-gui-tool-tips-are-your-friends/http://blinkdagger.com/tutorials/matlab/GUI/basic/uitable/part01/uitable_tutorial_01.ziphttp://www.mathworks.com/access/helpdesk/help/techdoc/index.html?/access/helpdesk/help/techdoc/ref/uitable.htmlhttp://blogs.mathworks.com/desktop/2008/06/02/tables-in-matlab-with-uitable/http://blogs.mathworks.com/videos/2009/01/29/exploring-uitable-properties/http://blinkdagger.com/matlab/matlab-gui-tool-tips-are-your-friends/
  • 7/31/2019 Hoc Matlab1 5691

    22/31

    Tool Tip for the add button:

    Tool tip for the first input parameter:

  • 7/31/2019 Hoc Matlab1 5691

    23/31

    Tool tip for the second input parameter:

    How to Add Tool TipsWithin the GUIDE editor, you can add a tool tip by modifying the TooltipString propertyusing the Property Inspector. Simply double click on the component to bring up the

    Property Inspector. The image below shows that the TooltipString property has been

    modified for the Add! button.

  • 7/31/2019 Hoc Matlab1 5691

    24/31

    Adding Tool Tips Programmatically

    If you would rather add the tool tips within your code, you can use the SET command. For

    instance, if you wanted to add a tool tip for the add button, you could do the following inthe opening function of the GUI:

    %put this code into the opening function of the GUIset(handles.input1_editText,'TooltipString','This is the first input.')set(handles.input2_editText,'TooltipString','This is the second input.')set(handles.add_pushbutton,'TooltipString','This is a tool tip! Pressto add.')

    If you have a large list of tooltips, you may want to do it in separate m-file and call that m-

    file in the opening function.

    Download the Source Files and Other LinksDownload the source files here.

    Yairs Undocumented Tips on How to Spice up Tooltips

    Pimp My Gui Part 3: Help Me Help Yourself

    Blinkdagger proudly presentsPimp My Gui, a series that will provide our readers with tipsand tricks on how they can make their GUIs beautiful on both the inside and outside.

    In the previous post, we talked about using a custom menu bar to retain

    functionality while clearing up space on a GUI. In this post, were going todiscuss how to implement a help feature for your GUI. The help feature

    should be informative and simple so that any person can learn how to use the

    GUI quickly. The help feature should offer other useful information such as the versionnumber, author, and other tidbits of information.

    In the last post, we ended up with this GUI, but we never did anything for the help

    portion of the GUI:

    http://blinkdagger.com/tutorials/matlab/GUI/basic/tooltip/tooltip-gui.ziphttp://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/http://blinkdagger.com/matlab/pimp-my-gui-part-3-help-me-help-yourself/http://blinkdagger.com/pimp-my-guihttp://blinkdagger.com/tutorials/matlab/GUI/basic/tooltip/tooltip-gui.ziphttp://undocumentedmatlab.com/blog/spicing-up-matlab-uicontrol-tooltips/http://blinkdagger.com/matlab/pimp-my-gui-part-3-help-me-help-yourself/http://blinkdagger.com/pimp-my-gui
  • 7/31/2019 Hoc Matlab1 5691

    25/31

    Contents

    How to use this GUI: Quick User Guide About this GUI

    Putting it all Together

    Addpath

    Next Time

    How to use this GUI: Quick User Guide

    http://blinkdagger.com/matlab/pimp-my-gui-part-3-help-me-help-yourself/#1http://blinkdagger.com/matlab/pimp-my-gui-part-3-help-me-help-yourself/#2http://blinkdagger.com/matlab/pimp-my-gui-part-3-help-me-help-yourself/#3http://blinkdagger.com/matlab/pimp-my-gui-part-3-help-me-help-yourself/#4http://blinkdagger.com/matlab/pimp-my-gui-part-3-help-me-help-yourself/#5http://blinkdagger.com/matlab/pimp-my-gui-part-3-help-me-help-yourself/#1http://blinkdagger.com/matlab/pimp-my-gui-part-3-help-me-help-yourself/#2http://blinkdagger.com/matlab/pimp-my-gui-part-3-help-me-help-yourself/#3http://blinkdagger.com/matlab/pimp-my-gui-part-3-help-me-help-yourself/#4http://blinkdagger.com/matlab/pimp-my-gui-part-3-help-me-help-yourself/#5
  • 7/31/2019 Hoc Matlab1 5691

    26/31

    A simple but effective way to implement a help screen is to create another standalone GUI.

    For example, I created a standalone GUI that consists of only a static text component. I

    populated the text field with some helpful information regarding the GUI and detailed asuccinct users guide.

    When the GUI is called by the user, the user will see the following:

  • 7/31/2019 Hoc Matlab1 5691

    27/31

    Of course, you can add more tidbits and information depending on the actual GUI. Youmight also want to add a FAQ section here if you feel your GUI needs some extra

    explanation. If you cant fit all the information into one screen, you can also utilize

    pushbuttons that will change the static text when the button is pressed.

    About this GUI

    I also created a stand alone GUI for the About. This part should inform the user about

    what version of MATLAB the GUI was designed on, date of creation, name of author,

    some contact information, and the version number of the GUI.

  • 7/31/2019 Hoc Matlab1 5691

    28/31

    When the GUI is called by the user, the user will see the following:

  • 7/31/2019 Hoc Matlab1 5691

    29/31

    Putting it all together

    All we have to do now is to call the appropriate help GUI in the appropriate callback, pretty

    straightforward stuff. When the menu items for the help menu was created, MATLAB

    automatically created callbacks for each menu item.

    the callback for the How to use this GUI menu item is:

    function helpGUI_Callback(hObject, eventdata, handles)%this is the name of the help GUI that contains the help informationhelpGUI_description

  • 7/31/2019 Hoc Matlab1 5691

    30/31

    the callback for the About this GUI menu item is

    function aboutGui_Callback(hObject, eventdata, handles)%this command calls the GUI that has the "about" informationaboutGUI_description

    Addpath

    I have found that the addpath command within MATLAB can really help you keep your

    files organized. If your GUI utilizes other m-files or images, it can be advantageous to store

    them in appropriate subdirectories.

    you can store them in a separate directory and simply add the path of that directory. It canget a little crowded in the main directory of your GUI, so its usually a good idea to put all

    your sub functions and sub GUIs in a lower level folder.

    For example, the following image shows how I originally had my m-files organized. The

    main GUI files are Tutor_Commuter_QQ_05.figand Tutor_Commuter_QQ_05.m. The restof the files are used in creating the help.

  • 7/31/2019 Hoc Matlab1 5691

    31/31

    In the following image, you can see how I reorganized the files. All the files except the

    main files are placed in thesubfunctions folder.

    Now, all that has to done is to add the path of the subfunctions within the OpeningFunction of the main GUI.

    %the addpath adds the subfunctions folder onto the MATLAB path. m-files%within this directory will be run as if it were in the same directoryas the%main GUI itself%the fullfile command combines directory names and parts%pwd is the "present working directory", which in this case will be

    where%the main GUI is locatedaddpath(fullfile(pwd,'subfunctions');

    Next Time

    It looks like weve revamped Zanes GUI so that its obtained a shade of respectability. If

    you guys have any more ideas on what can be done, wed love to hear about it. In the nearfuture, well open up the floor and ask for GUIs from our readers! Until next time . . .