Brief Guide to Creating a Matlab GUI Application by tjm72505

VIEWS: 0 PAGES: 3

									Brief Guide to Creating a Matlab GUI Application
Creating a GUI consists of two steps. Step 1 is to design the user interface including figures, buttons and
menus. Step 2 is to create the Matlab code that runs when buttons are pressed. The code goes in callback
functions that are activated each time a button is pressed. For an example of a GUI application, see
     >>graf2d
     >>edit graf2d
To make things simple, Matlab includes a GUIDE application that provides a WYSIWYG interface for
laying out components on the screen and creates stub callback functions. GUIDE is ideal for most simple
GUI applications. Start GUIDE from the Matlab command line
     >>guide
then use the online help for GUIDE to get started. Also see Matlab 7 manual, "Creating Graphical User
Interfaces" (PDF file, 480 pages), available on-line at the Mathworks web site. To see more samples, when
GUIDE starts, select the GUI with Uicontrols template and look at the generated code.

The following instructions are for Matlab 7. Other versions are similar.

Create the GUI
In the Matlab command window: >>guide
opens GUIDE quick start. Select Blank GUI..
File > Preferences > GUDE > Show names component palette.
View > Property Inspector
Click and drag to resize girded layout area to desired size, or use Position property in Property Inspector to
       set an exact size.
Tools > Application Options > Resize behavior = Proportional.
Add components.
Use Static Text component for labels.
Align components using the Align tool bar button.
Label components using String property.
Label figure using Name property
For PopupMenu, String property gets one line per menu entry.
Tag components, Tag property. E.g. "button_go"
Save GUI using Tools > Run (or Tools > Activate Figure in Mlab 6). Will save both a .fig figure file and a
       .m M file under the chosen name.


Create the Code
Place code that should run when the application starts in the opening function which is called
       guiname_OpeningFcn(). Place code after comment that begins % varagin… In Matlab 6, place
       opening code just before the line: if nargout > 0
Place code for buttons and other components in the appropriate callback function stubs that are at the end
       of the .m file.




matlab-gui.doc                                   Page 1 of 3                                          4/7/2007
Code Samples
set(handles.mass, 'String', mass); % set a text box
set(handles.text6, 'String', 'kg'); % set a text box
set(hObject, 'String', 0); % set own edit box
ns=str2double(get(handles.text_sides,'string')); % edit box data


% dealing with radio buttons that are in a button group
function unitgroup_SelectionChangeFcn(hObject, eventdata, handles)
if (hObject == handles.choice1)
    % choice 1 code here
else
    % other choice code here
end

% dealing with a checkbox
function checkbox1_Callback(hObject, eventdata, handles)
if (get(hObject,'Value') == 1)
   % checked code goes here
else
   % unchecked code goes here
end
set(handles.checkbox1,'Value',1) % sets checkbox
set(handles.checkbox1,'Value',0) % clears checkbox
if (get(handles.checkbox1,'Value') == 1) % check another checkbx

% creating plot and changing properties
h=plot(x,y,'k');
set(h, 'LineWidth',2);
set(h, 'LineStyle','--');




matlab-gui.doc                  Page 2 of 3                          4/7/2007
Notes
When the GUI is saved for the first time, GUIDE                   in the GUI. To plot to another window use
   uses the Callback property to create the                       h=figure(1); in the code
   callback file name. If Callback property is
   set to <automatic> then the Tag property                  In the callback use cla not clf.
   name is used for the callback. Therefore,
   one should set all the Tag properties before              Any error messages will appear in the Matlab
   saving the GUI. If the Tag is changed then                   command window.
   set Callback = <automatic> and save.
                                                             For callback to see variables from Matlab and
To access variables created in the initialization                 vice-versa, must have
    function or in any callback function from                     global myvar
    another callback function, store global data             in both Matlab and callback. Watch for name
    in the handles structure. For example:                        clashes.
         handles.mydata=mydata;
         guidata(h,handles);                                 To create dialog popups
    Then access in the callback by                               msgbox('mymsg')
         mydata=handles.mydata;                                  errordlg('My msg,'Bad Input','modal')
    where h=fig if in the initialization function                helpdlg('Msg','Title','modal')
    and h=h if in a callback
    Try >>help guihandles and >>help guidata                 Note: If when start GUIDE, the layout area is too
    for info                                                     small to get your mouse on it, right-click >
                                                                 Property Inspector > Position and set height.
Code placed in the opening function is run only
   at application launch time.                               Use a button group to manage a set of radio
                                                                 buttons
Callback for Edit text controls
    function
    edittext1_Callback(h,eventdata,handles,vara
    rgin)
    user_string = get(h,'string');

Edit text control to get a numeric value
     function
     edittext1_Callback(h,eventdata,handles,vara
     rgin)
     user_entry = str2double(get(h,'string'));
     if isnan(user_entry)
        errordlg('You must enter a numeric
     value','Bad Input','modal')
     end

To plot to a GUI with multiple axes
    axes(handles.axes1)
makes the axes with Tag property = axes1 the
    current axes. This does not seem to work
    when using step(), impulse(), bode() or other
    commands that automatically generate plots.
    Instead, generate points ([y,t]=step(sys);)
    and use plot(t,y) to plot.

Sometimes it is simpler to plot in an external plot
   window rather than to one or more plot axes




matlab-gui.doc                                      Page 3 of 3                                      4/7/2007

								
To top