zl程序教程

您现在的位置是:首页 >  其他

当前栏目

【MATLAB】基本绘图 ( 句柄值 | 对象句柄值获取 | 创建对象时获取句柄值 | 函数获取句柄值 | 获取 / 设置 对象属性 | 获取对象属性 )

属性MATLAB对象 函数 设置 获取 基本 绘图
2023-06-13 09:17:49 时间

文章目录

一、对象句柄值获取


1、句柄值

对象的句柄值 , 类似于编程时的引用 , 将对象的句柄值赋值给变量后 , 该变量就可以代表指定的绘图对象 ;

对象的 Handle 标识 ;

2、创建对象时获取句柄值

创建对象时获取图形对象句柄值 : 创建对象时 , 使用变量接收该对象 , 下面的代码就是使用 line_sin 变量获取 线 对象的句柄值 ;

line_sin = plot(x, y)

3、函数获取句柄值

使用函数获取对象句柄值 : 下面的函数是获取相关对象句柄值的函数 ;

gca : 获取当前坐标轴的句柄值 ;

gcf : 获取当前图形的句柄值 ;

allchild : 查找特定对象的所有子对象的句柄 ;

ancestor : 查找特定对象的父容器的句柄值 ;

delete : 删除对象 ;

findall : 找到所有的图形对象 ;

4、获取 / 设置 对象属性

获取某个对象的属性 : 使用 get 函数 , 可以获取某个对象的属性 ;

get()

设置某个对象的属性 : 使用 set 函数 , 可以设置某个对象的属性 ;

set()

二、获取对象属性


1、获取 线 对象属性

获取图形对象属性 :

代码示例 :

% x 轴变量
% 在 0 ~ 2pi 之间产生 1000 个数值
x = linspace(0, 2 * pi, 1000);

% y 轴变量
y = sin(x);

% 使用 h 变量接受 plot 函数绘制的曲线图像句柄值
h = plot(x, y);

% 获取曲线图像的属性
get(h)

绘制的曲线 :

命令行窗口打印的内容 : 获取的 h 变量 , 即 plot 函数绘制的曲线对象的各个属性 ;

  • Color : 颜色值
  • LineWidth : 线的粗细 , 线宽
>> Untitled2
    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: ''
             DeleteFcn: ''
           DisplayName: ''
      HandleVisibility: 'on'
               HitTest: 'on'
         Interruptible: 'on'
              LineJoin: 'round'
             LineStyle: '-'
             LineWidth: 0.5000
                Marker: 'none'
       MarkerEdgeColor: 'auto'
       MarkerFaceColor: 'none'
         MarkerIndices: [1×1000 uint64]
            MarkerSize: 6
                Parent: [1×1 Axes]
         PickableParts: 'visible'
              Selected: 'off'
    SelectionHighlight: 'on'
                   Tag: ''
                  Type: 'line'
         UIContextMenu: [0×0 GraphicsPlaceholder]
              UserData: []
               Visible: 'on'
                 XData: [1×1000 double]
             XDataMode: 'manual'
           XDataSource: ''
                 YData: [1×1000 double]
           YDataSource: ''
                 ZData: [1×0 double]
           ZDataSource: ''

>> 

2、获取 坐标轴 对象属性

使用 get(gca) 可以获取当前坐标轴对象的属性 ;

代码示例 :

% x 轴变量
% 在 0 ~ 2pi 之间产生 1000 个数值
x = linspace(0, 2 * pi, 1000);

% y 轴变量
y = sin(x);

% 使用 h 变量接受 plot 函数绘制的曲线图像句柄值
h = plot(x, y);

% 获取曲线图像的属性
%get(h)

% 获取坐标轴对象属性
get(gca)

绘制结果 :

命令行窗口输出结果 : 其中 XLim: [0 7] 表示 x 轴取值范围是

0 ~ 7 ;

>> Untitled2
                       ALim: [0 1]
                   ALimMode: 'auto'
     ActivePositionProperty: 'outerposition'
          AmbientLightColor: [1 1 1]
               BeingDeleted: 'off'
                        Box: 'on'
                   BoxStyle: 'back'
                 BusyAction: 'queue'
              ButtonDownFcn: ''
                       CLim: [0 1]
                   CLimMode: 'auto'
             CameraPosition: [3.5000 0 17.3205]
         CameraPositionMode: 'auto'
               CameraTarget: [3.5000 0 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
                  CreateFcn: ''
               CurrentPoint: [2×3 double]
            DataAspectRatio: [3.5000 1 1]
        DataAspectRatioMode: 'auto'
                  DeleteFcn: ''
                  FontAngle: 'normal'
                   FontName: 'Helvetica'
                   FontSize: 10
              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'
              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.7903 0.7903]
     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.0506 0.0532 0.0071 0.0202]
                      Title: [1×1 Text]
    TitleFontSizeMultiplier: 1.1000
            TitleFontWeight: 'normal'
                       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: [0 7]
                   XLimMode: 'auto'
                 XMinorGrid: 'off'
                 XMinorTick: 'off'
                     XScale: 'linear'
                      XTick: [0 1 2 3 4 5 6 7]
                 XTickLabel: {8×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: [-1 1]
                   YLimMode: 'auto'
                 YMinorGrid: 'off'
                 YMinorTick: 'off'
                     YScale: 'linear'
                      YTick: [-1 -0.8000 -0.6000 -0.4000 -0.2000 0 0.2000 0.4000 0.6000 0.8000 1]
                 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'

>>