⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 guiplot.m

📁 电子社08年的《精通MATLABG UI设计》一书的所有代码
💻 M
字号:
function guiplot(action)
%HNDLGRAF Demonstrates Handle Graphics for line plots in MATLAB.
%   This window allows you to assemble a string        
%   of MATLAB commands that results in a plot in       
%   the axis in the upper left corner.                 
%                                                      
%   By playing with the popup menus on the right       
%   side of the window, you can adjust the line        
%   style of the plot, the line width, the marker size,
%   and the color using Handle Graphics.               
%                                                      
%   The MiniCommand Window in the lower                
%   right shows the list of commands that create       
%   the plot. If you like, you can even directly edit  
%   the commands in the MiniCommand Window.            
%   Type control-return to execute code in the         
%   MiniCommand Window.                                
 
%   Ned Gulley, 6-21-93
%   Copyright 1984-2002 The MathWorks, Inc. 
%   $Revision: 5.12 $  $Date: 2002/04/15 03:32:23 $

if nargin<1,
    action='initialize';
end;

if strcmp(action,'initialize'),
    oldFigNumber=watchon;

    figNumber=figure( ...
        'Visible','off', ...
        'NumberTitle','off', ...
        'Name','图形与线条对象');
    axes( ...
        'Units','normalized', ...
        'Position',[0.07 0.45 0.60 0.50]);

    %===================================
    % 代码窗口 
    top=0.35;
    left=0.05;
    right=0.70;
    bottom=0.05;
    labelHt=0.05;
    spacing=0.005;
    %建立frame框架
    frmBorder=0.02;
    frmPos=[left-frmBorder bottom-frmBorder ...
        (right-left)+2*frmBorder (top-bottom)+2*frmBorder];
    uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',frmPos, ...
        'BackgroundColor',[0.50 0.50 0.50]);
    %编辑文本框 
    labelPos=[left top-labelHt (right-left) labelHt];
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',[0.50 0.50 0.50], ...
        'ForegroundColor',[1 1 1], ...
        'String','代码窗口');
    % 可编辑的文本框 
    mcwPos=[left bottom (right-left) top-bottom-labelHt-spacing];
    mcwHndl=uicontrol( ...
        'Style','edit', ...
        'HorizontalAlignment','left', ...
        'Units','normalized', ...
        'Max',10, ...
        'BackgroundColor',[1 1 1], ...
        'Position',mcwPos, ...
        'Callback','hndlgraf(''quickeval'')', ...
        'String',' % Press buttons at the right for example plots.');
    % 保存句柄数据 
    set(gcf,'UserData',mcwHndl);

    %====================================
    %按钮属性 
    labelColor=[0.8 0.8 0.8];
    top=0.95;
    bottom=0.05;
    left=0.75;
    yInitLabelPos=0.90;
    left=0.75;
    labelWid=0.20;
    labelHt=0.05;
    btnWid=0.20;
    btnHt=0.05;
    %静态标签与按钮之间的距离
    btnOffset=0.003;
    % 按钮与与下一个静态标签之间的距离
    spacing=0.05;

    %====================================
    % 控制区域的frame
    frmBorder=0.02;
    yPos=0.05-frmBorder;
    frmPos=[left-frmBorder yPos btnWid+2*frmBorder 0.9+2*frmBorder];
    h=uicontrol( ...
        'Style','frame', ...
        'Units','normalized', ...
        'Position',frmPos, ...
        'BackgroundColor',[0.50 0.50 0.50]);
    
    %====================================
    % 线条线型 
    btnNumber=1;
    yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
    labelStr=' 线 型 ';
    labelList=' - | --| :| -.| o| x| +| *| .';
    cmdList=strvcat( ...
        ' ',' set(hndl,''LineStyle'',''--'')', ...
        ' set(hndl,''LineStyle'','':'')', ...
        ' set(hndl,''LineStyle'',''-.'')', ...
        ' set(hndl,''Marker'',''o'')', ...
        ' set(hndl,''Marker'',''x'')', ...
        ' set(hndl,''Marker'',''+'')', ...
        ' set(hndl,''Marker'',''*'')', ...
        ' set(hndl,''Marker'',''.'')');
    callbackStr='hndlgraf eval';
    
    % 线型静态标签
    labelPos=[left yLabelPos-labelHt labelWid labelHt];
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',labelColor, ...
        'HorizontalAlignment','left', ...
        'String',labelStr);

    %线型下拉菜单属性 
    btnPos=[left yLabelPos-labelHt-btnHt-btnOffset btnWid btnHt];
    hndl1=uicontrol( ...
        'Style','popup', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelList, ...
        'Callback',callbackStr, ...
        'UserData',cmdList);

    %====================================
    % 线条的线宽设置
    btnNumber=2;
    yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
    labelStr='线 宽';
    labelList=' 0.5|1|2|3|4';
    cmdList=strvcat( ...
        ' ',' set(hndl,''LineWidth'',1)', ...
        ' set(hndl,''LineWidth'',2)', ...
        ' set(hndl,''LineWidth'',3)', ...
        ' set(hndl,''LineWidth'',4)');
    callbackStr='hndlgraf eval';
    
    % 线宽静态标签
    labelPos=[left yLabelPos-labelHt labelWid labelHt];
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',labelColor, ...
        'HorizontalAlignment','left', ...
        'String',labelStr);

    % 线宽下拉菜单属性 
    btnPos=[left yLabelPos-labelHt-btnHt-btnOffset btnWid btnHt];
    hndl2=uicontrol( ...
        'Style','popup', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelList, ...
        'Callback',callbackStr, ...
        'UserData',cmdList);

    %====================================
    % 线条的标记大小
    btnNumber=3;
    yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
    labelStr=' 标记大小';
    labelList=' 6| 9| 12| 15| 18';
    cmdList=strvcat( ...
        ' ',' set(hndl,''MarkerSize'',9)', ...
        ' set(hndl,''MarkerSize'',12)', ...
        ' set(hndl,''MarkerSize'',15)', ...
        ' set(hndl,''MarkerSize'',18)');
    callbackStr='hndlgraf eval';
    
    %标记大小静态标签
    labelPos=[left yLabelPos-labelHt labelWid labelHt];
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',labelColor, ...
        'HorizontalAlignment','left', ...
        'String',labelStr);

    %标记大小下拉式菜单
    btnPos=[left yLabelPos-labelHt-btnHt-btnOffset btnWid btnHt];
    hndl3=uicontrol( ...
        'Style','popup', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelList, ...
        'Callback',callbackStr, ...
        'UserData',cmdList);

    %====================================
    % 线条颜色设置
    btnNumber=4;
    yLabelPos=top-(btnNumber-1)*(btnHt+labelHt+spacing);
    labelStr=' 颜 色';
    labelList=' cyan| red| green| blue| magenta| yellow';
    cmdList=strvcat( ...
        ' set(hndl,''Color'',''cyan'')', ...
        ' set(hndl,''Color'',''red'')', ...
        ' set(hndl,''Color'',''green'')', ...
        ' set(hndl,''Color'',''blue'')', ...
        ' set(hndl,''Color'',''magenta'')', ...
        ' set(hndl,''Color'',''yellow'')', ...
        ' set(hndl,''Color'',''white'')');
    callbackStr='hndlgraf eval';
    
    % 颜色静态标签
    labelPos=[left yLabelPos-labelHt labelWid labelHt];
    uicontrol( ...
        'Style','text', ...
        'Units','normalized', ...
        'Position',labelPos, ...
        'BackgroundColor',labelColor, ...
        'HorizontalAlignment','left', ...
        'String',labelStr);

    % 颜色下拉菜单属性 
    btnPos=[left yLabelPos-labelHt-btnHt-btnOffset btnWid btnHt];
    hndl4=uicontrol( ...
        'Style','popup', ...
        'Units','normalized', ...
        'Position',btnPos, ...
        'String',labelList, ...
        'Callback',callbackStr, ...
        'UserData',cmdList);

    %====================================
    %相关信息
    uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',[left bottom+2*btnHt+spacing btnWid 2*btnHt], ...
        'String','信息', ...
        'Callback','hndlgraf(''info'')');

    % 关闭按钮
    uicontrol( ...
        'Style','pushbutton', ...
        'Units','normalized', ...
        'Position',[left bottom btnWid 2*btnHt], ...
        'String','关闭', ...
        'Callback','close(gcf)');

    % 不覆盖图形
    hndlList=[mcwHndl hndl1 hndl2 hndl3 hndl4];
    watchoff(oldFigNumber);
    set(figNumber,'Visible','on', ...
        'UserData',hndlList);
    hndlgraf('eval');

elseif strcmp(action,'info'),
    helpwin(mfilename)
   
elseif strcmp(action,'quickeval'),
     %执行代码窗口的代码程序
    hndlList=get(gcf,'UserData');
    mcwHndl=hndlList(1);
    evalmcw(mcwHndl);

elseif strcmp(action,'eval'),
    %组装和执行完成的命令
    
    hndlList=get(gcf,'UserData');
    n=length(hndlList);
    mcwHndl=hndlList(1);
    evalStr=strvcat(' x=0:0.02:1;',' hndl=plot(x,humps(x));');
    for count=2:n,
        newStrList=get(hndlList(count),'UserData');
        newStrVal=get(hndlList(count),'Value');
        newStr=deblank(newStrList(newStrVal,:));
        if ~isempty(newStr), evalStr=strvcat(evalStr,newStr); end;
    end;
    set(mcwHndl,'String',evalStr);
    evalmcw(mcwHndl);

end;    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -