主要内容

管理图表类的属性

When you develop a custom chart as a subclass of theChartContainer基础类,您可以使用某些技术使您的代码更加稳健,高效,并根据用户的需求进行量身定制。这些技术着重于您如何定义和管理类的属性。使用任何有助于您要创建的可视化类型和要提供的用户体验的类型。

  • Initialize property values— Set the default state of the chart in case your users call the implicit constructor without any input arguments.

  • Validate property values— Ensure that the values are valid before using them to perform a calculation or configure one of the underlying graphics objects in your chart.

  • 自定义属性显示— Provide a customized list of properties when a user references the chart object without semicolon.

  • 优化更新方法- 提高性能更新方法仅在耗时计算中使用您的属性子集时。

初始化属性值

分配您类的所有公共属性的默认值。如果用户调用构造函数方法时,则这样做会配置有效图表。

对于存储协调数据的属性,将初始值设置为values or empty arrays so that the default chart is empty when the user does not specify the coordinates. Choose the default coordinates according to the requirements of the plotting functions you plan to call in your class methods. To learn about the requirements, see the documentation for the plotting functions you plan to use.

验证属性值

一个好的做法是在代码使用这些值之前验证类属性的值。一种方便的方法是在定义它们时验证属性的大小和类别。例如,此属性块验证了四个属性的大小和类。

propertiesIsovalue(1,1)双= 0.5封闭{mustBeMember(Enclose,{'above','below'})}='以下'可挑剔的(1,1) matlab.lang.OnOffSwitchState='上'颜色(1,3)double {Mustbegreatthanorequal(颜色,0),。。。mustBeLessThanOrEqual(Color,1)}= [.2 .5 .8]结尾

  • Isovalue必须是1 x-1课程的阵列双倍的

  • Enclose必须有任何一个值'以上'or'以下'

  • 可挑剔的必须是1 x-1课程的阵列matlab.lang.onoffswitchstate

  • 颜色必须是1 x 3级的班级双倍的,每个值在范围内[0,1]

You can also validate properties that store the underlying graphics objects in your chart. To determine the class name of an object, call the corresponding plotting function at the command line, and then call the班级函数以获取类名称。例如,如果您打算致电patch在您的功能设置方法,,,,Call thepatch带有输出参数的命令行的功能(输入参数无关紧要)。然后将输出传递给班级功能以获取其类名称。

x = patch(nan,nan,nan);类(x)
ans = 'matlab.graphics.primitive.Patch'

使用输出班级函数以验证类中相应属性的类。例如,以下每个属性存储修补object.

properties(访问=私有,瞬态,不可复制)isopatch(1,1)matlab.graphics.primitive.patchCapPatch(1,1)matlab.graphics.primitive.patch结尾

有时候,您可能需要定义一个属性that can store different shapes and classes of values. For example, if you define a property that can store a character vector, cell array of character vectors, or string array, omit the size and class validation or use a custom property validation method.

有关验证属性的更多信息,请参见验证属性值

自定义属性显示

将图表定义为一个子类的好处之一ChartContainerbase class is that it also inherits from thematlab.mixin.CustomDisplay班级。因此,您可以自定义属性列表matlab®当您在没有分号的情况下引用图表时,在命令窗口中显示。要自定义属性显示,请超载getPropertyGroups方法。在该方法中,您可以自定义列出了哪些属性以及列表的顺序。例如,考虑一个isosurfcapchart具有以下公共属性的类。

propertiesIsovalue(1,1)双= 0.5封闭{mustBeMember(Enclose,{'above','below'})}='以下'可挑剔的(1,1) matlab.lang.OnOffSwitchState='上'颜色(1,3)double {Mustbegreatthanorequal(颜色,0),。。。mustBeLessThanOrEqual(Color,1)}= [.2 .5 .8]结尾

The followinggetPropertyGroups方法将标量对象属性列表指定为颜色,,,,Isovalue,,,,Enclose,,,,and可挑剔的

functionpropgrp = getPropertyGroups(obj)如果~isscalar(obj)一系列对象列表propgrp = getPropertyGroups@matlab.mixin.customdisplay(obj);别的% List for scalar objectpropList = {'颜色',,,,'IsoValue',,,,'括',,,,'CapVisible'}; propgrp = matlab.mixin.util.PropertyGroup(propList);结尾结尾

When the user references an instance of this chart without a semicolon, MATLAB displays the customized list.

C=isosurfcapchart
C=isosurfcapchartwith properties: Color: [0.2000 0.5000 0.8000] IsoValue: 0.5000 Enclose: 'below' CapVisible: on

有关自定义属性显示的更多信息,请参阅自定义属性显示

优化更新方法

In most cases, the更新方法of your class reconfigures all the relevant aspects of your chart that depend on the public properties. Sometimes, the reconfiguration involves an expensive calculation that is time consuming. If the calculation involves only a subset of the properties, you can design your class to execute that code only when it is necessary.

优化的一种方法更新方法是将这些组件添加到您的课程中:

  • 定义一个名为的私人财产昂贵的旋转接受一个逻辑value. This property indicates whether any of the properties used in the expensive calculation have changed.

  • Write a涉及昂贵计算的每个属性的方法。在每个内部方法,设置昂贵的旋转财产为true

  • 编写一种执行昂贵计算的受保护方法。

  • 更新方法that checks the value of昂贵的旋转。如果值为true,执行执行昂贵计算的方法。

以下代码提供了此设计的简化实现。

ClassDef优化的Chart propertiesProp1 Prop2结尾properties(访问=私有,瞬态,不可复制)昂贵的旋转(1,1)逻辑=true结尾方法(Access = protected)function设置(OBJ)%配置图表% ...结尾function更新(OBJ)%执行昂贵的计算如果需要如果OBJ。廉价换成Do ExpensiveCalculation(OBJ);obj.expentypropchanged = false;结尾%更新图表的其他方面% ...结尾functionDO ExpensiveCalculation(OBJ)%昂贵的代码% ...结尾结尾方法functionset.prop2(obj,val)obj.prop2 = val;obj.expentivepropchanged = true;结尾结尾结尾
在这种情况下,Prop2是involved in the expensive calculation. The放。Prop2方法设置值Prop2,然后设置昂贵的旋转totrue。因此,下一次更新方法运行,它调用doExpensiveCalculation除非昂贵的旋转true。然后更新方法继续更新图表的其他方面。

示例:带有自定义属性显示的优化的等法表图

定义isosurfcapchart展示一个课程是osurfacewith the associatedIsocaps。包括以下功能:

  • 使用大小和类验证的属性

  • 定制属性显示

  • 优化更新重新计算的方法是osurfaceandIsocaps只有一个或多个相关属性更改

To define this class, create a program file namedisosurfcapchart。m在MATLAB路径上的文件夹中。然后按照表中的步骤来实现类。

执行

Derive from theChartContainer基类。

ClassDefisosurfcapchart< matlab.graphics.chartcontainer.ChartContainer

使用类和大小验证定义公共属性。

  • 体积,,,,Isovalue,,,,and颜色are parameters for the是osurface

  • Enclose,,,,哪个卡普兰,,,,and可挑剔的are parameters for theIsocaps

属性体积双倍的=兰德(25,25,25)Isovalue(1,1)双= 0.5封闭{mustBeMember(Enclose,{'以上',,,,'以下'})} ='以下'哪个卡普兰{mustBeMember(WhichCapPlane,{'all','xmin',。。。'xmax',,,,'ymin',,,,'ymax',,,,'zmin',,,,'zmax'})} ='全部'可挑剔的(1,1) matlab.lang.OnOffSwitchState ='上'颜色(1,3) double{Mustbegreatthanorequal(颜色,0),。。。mustBeLessThanOrEqual(Color,1)} = [.2。5.8]结尾

定义私人属性。

  • IsoPatchandCapPatch存储修补objects for the是osurfaceandIsocaps

  • SmoothData存储卷数据的平滑版本。

  • 昂贵的旋转indicates whether the update method needs to recalculate the是osurfaceandIsocaps

属性(Access =私有,瞬态,不可复制)isopatch(1,1)matlab.graphics.graphics.primitive.patch cappatch(1,1)matlab.grabs.graphics.praphics.primitive.patch Smoothdata双倍的=[];昂贵的Propchanged(1,1)逻辑= true结尾

实施设置方法。在这种情况下,Call thepatchfunction twice to create the修补objects for the是osurfaceandIsocaps。将对象存储在相应的属性中,并配置轴。

方法(访问=受保护)function设置(obj)ax = getaxes(obj);%创建两个补丁对象obj.isopatch = patch(ax,nan,nan,nan,'edgeColor',,,,'没有任何',,,,。。。“ faceColor”,,,,[.2 .5 .8],'FaceAlpha',0.9);保持(斧头,'上');obj.CapPatch = patch(ax,NaN,NaN,NaN,'edgeColor',,,,'没有任何',,,,。。。“ faceColor”,,,,'interp');%配置轴查看(AX,3)CAMLIGHT(AX,'无穷');Camlight(ax,'left');照明(斧头,'gouraud');保持(斧头,'离开');结尾

实施更新方法。Decide whether to call thedoExpensiveCalculation方法by testing the value of昂贵的旋转。然后继续更新图表的其他(较便宜)方面。

function更新(obj)%执行昂贵的计算如果需要如果OBJ。廉价换成Do ExpensiveCalculation(OBJ);obj.expentypropchanged = false;结尾%更新Cappatch的可见性和更新颜色obj.CapPatch.Visible = obj.CapVisible; obj.IsoPatch.FaceColor = obj.Color;结尾

实施doExpensiveCalculation方法,,,,which smooths the volume data and recalculates the faces and vertices of the是osurfaceandIsocaps

functionDO ExpensiveCalculation(OBJ)%更新isosurfaceobj.smoothdata = smooth3(obj.volumedata,,'box',7);[f,v] = isosurface(obj.smoothdata,obj.isovalue);设置(obj.isopatch,“面孔”,,,,F,'Vertices',v);等法(obj.smoothdata,obj.isopatch);%更新Isocaps[m,n,p] = size(obj.smoothdata);[XC,YC,ZC] = meshgrid(1:n,1:m,1:p);[fc,vc,cc] = isocaps(xc,yc,zc,obj.smoothdata,obj.isovalue,。。。obj.Enclose,obj.WhichCapPlane); set(obj.CapPatch,“面孔”,fc,'Vertices',VC,'cdata',,,,Cc);结尾

实施getPropertyGroups自定义属性显示的方法。

functionpropgrp = getPropertyGroups(obj)如果~isscalar(obj)一系列对象列表propgrp = getPropertyGroups@matlab.mixin.customdisplay(obj);别的% List for scalar objectpropList = {'颜色',,,,'IsoValue',,,,'括',,,,'CapVisible',,,,。。。'WhichCapPlane',,,,'VolumeData'}; propgrp = matlab.mixin.util.PropertyGroup(propList);结尾结尾结尾

实施每个昂贵属性的方法(体积,,,,Isovalue,,,,andEnclose)。在每个方法中,设置相应的属性值,然后设置昂贵的旋转totrue

方法function放。体积(obj,val) obj.VolumeData = val; obj.ExpensivePropChanged = true;结尾functionset.isovalue(obj,val)obj.isovalue = val;obj.expentivepropchanged = true;结尾function放。Enclose(obj, val) obj.Enclose = val; obj.ExpensivePropChanged = true;结尾结尾结尾

接下来,创建一系列卷数据,然后创建一个实例isosurfcapchart

[X,Y,Z] = meshgrid(-2:0.1:2); v = (1/9)*X.^2 + (1/16)*Y.^2 + Z.^2; c = IsoSurfCapChart('VolumeData',v,'IsoValue',,,,0.5)
C=isosurfcapchartwith properties: Color: [0.2000 0.5000 0.8000] IsoValue: 0.5000 Enclose: 'below' CapVisible: on WhichCapPlane: 'all' VolumeData: [41×41×41 double]

改变颜色C并隐藏Isocaps

C.Color = [1 0.60 0];c.apvisible = false;

也可以看看

Classes

功能

Related Topics