Matlab 版 (精华区)

发信人: candle ( 马 走 日), 信区: Matlab
标  题: GUI FAQ  (from Mathworks)
发信站: 紫 丁 香 (Mon Dec 27 22:00:54 1999), 转信

Problem Description
    I want to build a Graphical User Interface (GUI) that waits for input fr
om a
    user. How can I create a modal figure that pauses the execution of an M-
file to
    prompt a user for input?
Solution:
    There are several ways to program GUIs to wait for user input. The first
 part
    of this solution addresses MATLAB 5 and the end of this solution is for
    MATLAB 4 users.
    Method 1: UIWAIT
    This method involves writing a function which creates the GUI manually. 
This
    type of capability is not currently available through GUIDE. Here is a q
uick
    example.
    The following M-File creates a new figure with an editable text box and 
a
    push button. The function creates these objects and then makes a call to
 the
    UIWAIT function. UIWAIT tells the function to pause its execution until 
a
    certain condition is met. In this case, the input argument f (the handle
 to the
    GUI figure) tells UIWAIT to pause execution until either "f" is closed o
r until
    "uiresume(f)" is called. Pressing the "Done" pushbutton accomplishes two

    tasks:
    - Causes the string property of the editable text box to be updated inte
rnally.
    - Its CallBack "uiresume(f)" continues the execution of the M-File.
        %% Beginning of M-File GETINFO %%
        function out = getinfo;
        f = figure('Units','Normalized',...
           'Position',[.4 .4 .3 .3],...
           'NumberTitle','off',...
           'Name','Info');
        e = uicontrol('Style','Edit',...
           'Units','Normalized',...
           'Position',[.1 .4 .3 .1],...
           'Tag','myedit');
        p = uicontrol('Style','PushButton',...
           'Units','Normalized',...
           'Position',[.6 .4 .3 .1],...
           'String','Done',...
           'CallBack','uiresume(gcbf)');
        uiwait(f)
        out = str2num(get(e,'String'));
        close(f)
    To call GETINFO from an M-File or the Command Line:
        a = getinfo
    This example can be easily extended to include more edit boxes.
    Method 2: Use one of MATLAB's custom dialog box functions
    Here is a list of the dialog box functions shipped with MATLAB that can 
be
    used along with UIWAIT, as of MATLAB 5.3:
    dialog - Create dialog figure.
    axlimdlg - Axes limits dialog box.
    errordlg - Error dialog box.
    helpdlg - Help dialog box.
    inputdlg - Input dialog box.
    listdlg - List selection dialog box.
    menu - Generate menu of choices for user input.
    msgbox - Message box.
    questdlg - Question dialog box.
    warndlg - Warning dialog box.
    uigetfile - Standard open file dialog box.
    uiputfile - Standard save file dialog box.
    uisetcolor - Color selection dialog box.
    uisetfont - Font selection dialog box.
    pagedlg - Page position dialog box.
    pagesetupdlg - Page setup dialog.
    printpreview - Display preview of figure to be printed.
    printdlg - Print dialog box.
    waitbar - Display wait bar.
    For example:
        h=helpdlg('Push OK to plot peaks');
        uiwait(h);
        peaks;
    If you are using MATLAB 4, some of the features mentioned above are not
    available. Here some suggestions with respect to MATLAB 4:
    In general, we recommend that you not make the GUI wait. Instead, you sh
ould
    break the program into several smaller steps or programs. For example, t
he
    following M-file, bgui.m builds a GUI that evaluates the input in an edi
t
    uicontrol:
        function bgui(state)
        if nargin == 0  % Build the GUI
        % Create the Figure Window
        figure('Units','normal','Position',[.25 .25 .5 .5], ...
              'Name','BGUI Example','NumberTitle','off', ...
              'Color','w');
        % Set the default units for uicontrols to 'normal'.
        % This makes it easier to place the uicontrols, and
        % the increase/decrease in size if the figure is
        % resized.
        set(gcf,'DefaultUicontrolUnits','normal')
        % Add a static text uicontrol
        st = uicontrol('Style','text','String','Enter Command:', ...
                      'Position',[.1 .8 .8 .1], ...
                      'BackgroundColor','w','ForegroundColor','k', ...
                      'HorizontalAlignment','left');
        % Add the edit uicontrol
        h = uicontrol('Style','edit','Position',[.1 .6 .8 .2], ...
                     'min',0,'max',2)
        % Add the push button
        i = uicontrol('Style','push','Position',[.1 .2 .8 .3], ...
                     'String','Evaluate Edit Block', ...
                     'CallBack','bgui(''evaluate'')');
        % Store the handle to the edit block in the figures
        % UserData property.  This is done to avoid the use of
        % global variable.  See the UserData section in the
        % Building a Graphical User Interface Guide for more
        % information.
        set(gcf,'UserData',h)
        elseif strcmp(state,'evaluate') % Push button selected
        % Get the handle to the edit uicontrol
        h = get(gcf,'UserData');
        % Extract the command
        com = get(h,'String');
        % Evaluate the string using eval(try,catch)
        eval(com,'error(''Invalid MATLAB Command'')')
        end
    By using this method, the GUI is never in a paused state. When bgui is c
alled,
    it builds the GUI and ends. When the push button is selected, it calls b
gui using
    'evaluate' as the input. This will execute only the portion of code asso
ciated
    with the 'evaluate' in the IF statements.
    This method is known as switch-yard programming because the GUI is drivi
ng
    the program. Based on the action of the GUI, it will call the same funct
ion
    using different inputs to "switch" between tasks.
    An alternative method is to use a polling loop to pause the M-file. Belo
w is a
    brief example of how to do this:
        % Create the Figure Window
        figure('Units','normal','Position',[.25 .25 .5 .5], ...
              'Name','BGUI Example','NumberTitle','off', ...
              'Color','w');
        % Set the default units for uicontrols to 'normal'.
        % This makes it easier to place the uicontrols, and
        % the increase/decrease in size if the figure is
        % resized.
        set(gcf,'DefaultUicontrolUnits','normal')
        % Add a static text uicontrol
        st = uicontrol('Style','text','String','Enter Command:', ...
                      'Position',[.1 .8 .8 .1], ...
                      'BackgroundColor','w','ForegroundColor','k', ...
                      'HorizontalAlignment','left');
        % Add the edit uicontrol
        h = uicontrol('Style','edit','Position',[.1 .6 .8 .2], ...
                     'min',0,'max',2)
        % Add the push button
        i = uicontrol('Style','push','Position',[.1 .2 .8 .3], ...
                     'String','Evaluate Edit Block', ...
                     'CallBack','set(gcf,''UserData'',1)');
        % Start the polling loop
        while get(gcf,'UserData')~=1
          drawnow
        end
        eval(get(h,'String'),'disp(''Invalid Command'')')
    In this method, the WHILE loop continues to execute until the push butto
n is
    selected. Note that if you are using a PC, background processing must be

    enabled. To enable background processing, click on the Option menu in th
e
    Command Window and select Enable Background Processing. If Disable
    Background Processing is displayed, then background processing is alread
y
    enabled. 

--
风来疏竹,风过而竹不留声;
雁度寒潭,雁去而潭不留影。
故君子事来而心始现,事去而心随空。

※ 来源:.紫 丁 香 bbs.hit.edu.cn.[FROM: 150.59.34.186]
[百宝箱] [返回首页] [上级目录] [根目录] [返回顶部] [刷新] [返回]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:6.059毫秒