Matlab_GUI study notes (1) - basic concepts and operations of objects

Matlab_GUI study notes (1) - basic concepts and operations of objects


1. Objects and handles

Take figure as an example, enter on the command line:

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

The resulting

image can be seen as a sub-object of the desktop, the axes are sub-objects of the figure, and the function curve and "example" title are sub-objects of the axes

  • gcf
    is the handle of the current figure, similar to ans, you can assign gcf to the variable fig, and then call the sub-object of the current figure object through fig

  • allchild
    Use the doc command to view the allchild document as follows, and return the handles of all child objects of the object.

    Call allchild to view all child objects of the current 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
    gets all the properties of the object, call the get function to view all the properties of 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
    operates on the properties of an object, call the set function to change the position (position) property of the coordinate axis Axes, you can see from the above properties that the initial value of the position property is Position: [0.1300 0.1100 0.7750 0.8150], change its height and width below

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

    The changed figure becomes like this

  • findobj
    finds the handle to the desired object. For example, to find the handle of the title text "Example" above, you can write

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

    You can get multiple static text objects in the video tutorial, but I am using matlab2019a. Maybe because of the version, gca is the 'line' attribute, and gcf is the 'Axes' attribute

    >> 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: ''
    

    Via findobj(allchild(gca), 'String', '示例'), findobj(allchild(gcf), 'type', 'text'), findobj('type', 'text'), findobj('String', '示例'), findobj(gca,'Type','text','String','示例')none found a handle to the title "example" object. If you know its handle, you can set the attributes of static text, such as font, color, font size, etc., through the set function.

2. Object operation example

Create a simple color dynamic function graph by calling the function directly and setting the object properties

%界面
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

Use get, set, findobj, doc and other functions to understand and run this code.

Guess you like

Origin blog.csdn.net/Ucarrot/article/details/110387036