Matlab_GUI学习笔记(一)——对象的基本概念及操作

Matlab_GUI学习笔记(一)——对象的基本概念及操作


1. 对象与句柄

以figure为例,在命令行输入:

>>plot([0:10])
>>title('示例')

得到

图像可以看作是桌面的子对象,坐标轴是figure的子对象,函数曲线和“示例”标题是坐标轴的子对象

  • gcf
    为当前figure的句柄,类似ans,可以将gcf赋值给变量fig,再通过fig调用当前figure对象的子对象

  • allchild
    用doc命令查看allchild文档如下,返回对象的所有子对象的句柄。

    调用allchild查看当前figure的所有子对象

    >>allchild(gcf)
    
     ans = 
     11×1 graphics 数组:
    
    		Menu              (figMenuHelp)
    		Menu              (figMenuWindow)
    		Menu              (figMenuDesktop)
    		Menu              (figMenuTools)
    		Menu              (figMenuInsert)
    		Menu              (figMenuView)
    		Menu              (figMenuEdit)
    		Menu              (figMenuFile)
    		Toolbar           (FigureToolBar)
    		AnnotationPane
    		Axes              (示例)
    
  • get
    获得对象的所有属性,调用get函数查看Axes所有属性

    >>get(ans(11))
    						 ALim: [0 1]
    	                   ALimMode: 'auto'
    	     ActivePositionProperty: 'outerposition'
    	                 AlphaScale: 'linear'
    	                   Alphamap: [1×64 double]
    	          AmbientLightColor: [1 1 1]
    	               BeingDeleted: 'off'
    	                        Box: 'on'
    	                   BoxStyle: 'back'
    	                 BusyAction: 'queue'
    	              ButtonDownFcn: ''
    	                       CLim: [0 1]
    	                   CLimMode: 'auto'
    	             CameraPosition: [6 5 17.3205]
    	         CameraPositionMode: 'auto'
    	               CameraTarget: [6 5 0]
    	           CameraTargetMode: 'auto'
    	             CameraUpVector: [0 1 0]
    	         CameraUpVectorMode: 'auto'
    	            CameraViewAngle: 6.6086
    	        CameraViewAngleMode: 'auto'
    	                   Children: [1×1 Line]
    	                   Clipping: 'on'
    	              ClippingStyle: '3dbox'
    	                      Color: [1 1 1]
    	                 ColorOrder: [7×3 double]
    	            ColorOrderIndex: 2
    	                 ColorScale: 'linear'
    	                   Colormap: [64×3 double]
    	                  CreateFcn: ''
    	               CurrentPoint: [2×3 double]
    	            DataAspectRatio: [5 5 1]
    	        DataAspectRatioMode: 'auto'
    	                  DeleteFcn: ''
    	                  FontAngle: 'normal'
    	                   FontName: 'Helvetica'
    	                   FontSize: 10
    	               FontSizeMode: 'auto'
    	              FontSmoothing: 'on'
    	                  FontUnits: 'points'
    	                 FontWeight: 'normal'
    	                  GridAlpha: 0.1500
    	              GridAlphaMode: 'auto'
    	                  GridColor: [0.1500 0.1500 0.1500]
    	              GridColorMode: 'auto'
    	              GridLineStyle: '-'
    	           HandleVisibility: 'on'
    	                    HitTest: 'on'
    	               Interactions: [1×1 matlab.graphics.interaction.interface.DefaultAxesInteractionSet]
    	              Interruptible: 'on'
    	    LabelFontSizeMultiplier: 1.1000
    	                      Layer: 'bottom'
    	                     Legend: [0×0 GraphicsPlaceholder]
    	             LineStyleOrder: '-'
    	        LineStyleOrderIndex: 1
    	                  LineWidth: 0.5000
    	             MinorGridAlpha: 0.2500
    	         MinorGridAlphaMode: 'auto'
    	             MinorGridColor: [0.1000 0.1000 0.1000]
    	         MinorGridColorMode: 'auto'
    	         MinorGridLineStyle: ':'
    	                   NextPlot: 'replace'
    	              OuterPosition: [0 0 1 1]
    	                     Parent: [1×1 Figure]
    	              PickableParts: 'visible'
    	         PlotBoxAspectRatio: [1 0.7882 0.7882]
    	     PlotBoxAspectRatioMode: 'auto'
    	                   Position: [0.1300 0.1100 0.7750 0.8150]
    	                 Projection: 'orthographic'
    	                   Selected: 'off'
    	         SelectionHighlight: 'on'
    	                 SortMethod: 'childorder'
    	                        Tag: ''
    	                    TickDir: 'in'
    	                TickDirMode: 'auto'
    	       TickLabelInterpreter: 'tex'
    	                 TickLength: [0.0100 0.0250]
    	                 TightInset: [0.0367 0.0527 0.0136 0.0544]
    	                      Title: [1×1 Text]
    	    TitleFontSizeMultiplier: 1.1000
    	            TitleFontWeight: 'normal'
    	                    Toolbar: [1×1 AxesToolbar]
    	                       Type: 'axes'
    	              UIContextMenu: [0×0 GraphicsPlaceholder]
    	                      Units: 'normalized'
    	                   UserData: []
    	                       View: [0 90]
    	                    Visible: 'on'
    	                      XAxis: [1×1 NumericRuler]
    	              XAxisLocation: 'bottom'
    	                     XColor: [0.1500 0.1500 0.1500]
    	                 XColorMode: 'auto'
    	                       XDir: 'normal'
    	                      XGrid: 'off'
    	                     XLabel: [1×1 Text]
    	                       XLim: [1 11]
    	                   XLimMode: 'auto'
    	                 XMinorGrid: 'off'
    	                 XMinorTick: 'off'
    	                     XScale: 'linear'
    	                      XTick: [1 2 3 4 5 6 7 8 9 10 11]
    	                 XTickLabel: {11×1 cell}
    	             XTickLabelMode: 'auto'
    	         XTickLabelRotation: 0
    	                  XTickMode: 'auto'
    	                      YAxis: [1×1 NumericRuler]
    	              YAxisLocation: 'left'
    	                     YColor: [0.1500 0.1500 0.1500]
    	                 YColorMode: 'auto'
    	                       YDir: 'normal'
    	                      YGrid: 'off'
    	                     YLabel: [1×1 Text]
    	                       YLim: [0 10]
    	                   YLimMode: 'auto'
    	                 YMinorGrid: 'off'
    	                 YMinorTick: 'off'
    	                     YScale: 'linear'
    	                      YTick: [0 1 2 3 4 5 6 7 8 9 10]
    	                 YTickLabel: {11×1 cell}
    	             YTickLabelMode: 'auto'
    	         YTickLabelRotation: 0
    	                  YTickMode: 'auto'
    	                      ZAxis: [1×1 NumericRuler]
    	                     ZColor: [0.1500 0.1500 0.1500]
    	                 ZColorMode: 'auto'
    	                       ZDir: 'normal'
    	                      ZGrid: 'off'
    	                     ZLabel: [1×1 Text]
    	                       ZLim: [-1 1]
    	                   ZLimMode: 'auto'
    	                 ZMinorGrid: 'off'
    	                 ZMinorTick: 'off'
    	                     ZScale: 'linear'
    	                      ZTick: [-1 0 1]
    	                 ZTickLabel: ''
    	             ZTickLabelMode: 'auto'
    	         ZTickLabelRotation: 0
    	                  ZTickMode: 'auto'
    
  • set
    对一个对象的属性进行操作,调用set函数改变坐标轴Axes的位置(position)属性,由上面属性查看可以看到position属性初始值为Position: [0.1300 0.1100 0.7750 0.8150],下面更改其高度与宽度

    >>set(ans(11), 'position', [0.1300 0.1100 0.5 0.5])
    

    更改后的figure变成下面这个样子

  • findobj
    找到所需要的对象的句柄。比如要找到上面的标题文本“示例”的句柄,可以这样写

    findobj(allchild(gca), 'type', 'Text')
    

    视频教程上可以得到多个静态文本对象,但我使用的是matlab2019a可能因为版本的原因,gca为’line’属性,gcf为’Axes’属性

    >> allchild(gca)
    
    ans = 
    
      Line - 属性:
    
                  Color: [0 0.4470 0.7410]
              LineStyle: '-'
              LineWidth: 0.5000
                 Marker: 'none'
             MarkerSize: 6
        MarkerFaceColor: 'none'
                  XData: [1 2 3 4 5 6 7 8 9 10 11]
                  YData: [0 1 2 3 4 5 6 7 8 9 10]
                  ZData: [1×0 double]
    
      显示 所有属性
    
        AlignVertexCenters: 'off'
                Annotation: [1×1 matlab.graphics.eventdata.Annotation]
              BeingDeleted: 'off'
                BusyAction: 'queue'
             ButtonDownFcn: ''
                  Children: [0×0 GraphicsPlaceholder]
                  Clipping: 'on'
                     Color: [0 0.4470 0.7410]
                 CreateFcn: ''
           DataTipTemplate: [1×1 matlab.graphics.datatip.DataTipTemplate]
                 DeleteFcn: ''
               DisplayName: ''
          HandleVisibility: 'on'
                   HitTest: 'on'
             Interruptible: 'on'
                  LineJoin: 'round'
                 LineStyle: '-'
                 LineWidth: 0.5000
                    Marker: 'none'
           MarkerEdgeColor: 'auto'
           MarkerFaceColor: 'none'
             MarkerIndices: [1 2 3 4 5 6 7 8 9 10 11]
                MarkerSize: 6
                    Parent: [1×1 Axes]
             PickableParts: 'visible'
                  Selected: 'off'
        SelectionHighlight: 'on'
                       Tag: ''
                      Type: 'line'
             UIContextMenu: [0×0 GraphicsPlaceholder]
                  UserData: []
                   Visible: 'on'
                     XData: [1 2 3 4 5 6 7 8 9 10 11]
                 XDataMode: 'auto'
               XDataSource: ''
                     YData: [0 1 2 3 4 5 6 7 8 9 10]
               YDataSource: ''
                     ZData: [1×0 double]
               ZDataSource: ''
    

    通过findobj(allchild(gca), 'String', '示例')findobj(allchild(gcf), 'type', 'text')findobj('type', 'text')findobj('String', '示例')findobj(gca,'Type','text','String','示例')均未找到title“示例”对象的句柄。如果知道其句柄,可以通过set函数设置静态文本的属性,比如字体,颜色,字号等。

2. 对象操作实例

通过直接调用函数并设置对象属性,创建一个简单的颜色动态函数曲线图

%界面
h = figure('Units','normalized',...		%单位归一化,使在0-1之间
    'Position', [0.2,0.2,0.5,0.5],...
    'Menu', 'none');

ha = axes('parent', h, 'Units', 'normalized',...		%坐标轴的父对象为图像figure
    'position',[0.1,0.1,0.8,0.8]);

hl = line('parent', ha, 'XData', [0:0.01:6],...			%函数曲线的父对象为坐标轴axes
    'YData', sin([0:0.01:6]), 'Color', 'r');

cstring = 'gbkay';
for k = 1:5
    set(hl, 'Color', cstring(k));
    pause(3);
end

使用get,set,findobj,doc等函数读懂并运行这段代码。

猜你喜欢

转载自blog.csdn.net/Ucarrot/article/details/110387036
今日推荐