![eb0bbe9d35b8ebeceda8c99507276d98.png](https://i-blog.csdnimg.cn/blog_migrate/776a98c3a16370be59f6f892cfdc9d6f.jpeg)
本示例说明如何仅使用属性子集来定义具有自定义属性的图表类。以下代码演示了如何重载matlab.mixin.CustomDisplay
类的getPropertyGroups
方法。该示例还演示了派生自matlab.graphics.chartcontainer.ChartContainer
基类的图表基本编码模式。我们可以使用此示例来熟悉图表开发的编码技术,或作为计划开发的类的基础。
要定义该类,请将以下代码复制到编辑器中,并将其名称保存SmoothPlotCustomDisplay.m
在可写文件夹中。
classdef SmoothPlotCustomDisplay < matlab.graphics.chartcontainer.ChartContainer
properties
XData (1,:) double = NaN
YData (1,:) double = NaN
SmoothColor (1,3) double {mustBeGreaterThanOrEqual(SmoothColor,0),...
mustBeLessThanOrEqual(SmoothColor,1)} = [0.9290 0.6940 0.1250]
SmoothWidth (1,1) double = 2
end
properties(Access = private,Transient,NonCopyable)
OriginalLine (1,1) matlab.graphics.chart.primitive.Line
SmoothLine (1,1) matlab.graphics.chart.primitive.Line
end
methods(Access = protected)
function setup(obj)
% Get the axes
ax = getAxes(obj);
% Create the original and smooth lines
obj.OriginalLine = plot(ax,NaN,NaN,'LineStyle',':');
hold(ax,'on')
obj.SmoothLine = plot(ax,NaN,NaN,'LineStyle','-',...
'Color',[0.9290 0.6940 0.1250],'LineWidth',2);
hold(ax,'off')
end
function update(obj)
% Update line data
obj.OriginalLine.XData = obj.XData;
obj.OriginalLine.YData = obj.YData;
obj.SmoothLine.XData = obj.XData;
obj.SmoothLine.YData = createSmoothData(obj);
% Update line color and width
obj.SmoothLine.Color = obj.SmoothColor;
obj.SmoothLine.LineWidth = obj.SmoothWidth;
end
function propgrp = getPropertyGroups(obj)
if ~isscalar(obj)
% List for array of objects
propgrp = getPropertyGroups@matlab.mixin.CustomDisplay(obj);
else
% List for scalar object
propList = {'SmoothColor','XData','YData'};
propgrp = matlab.mixin.util.PropertyGroup(propList);
end
end
function sm = createSmoothData(obj)
% Calculate smoothed data
v = ones(1,10)*0.1;
sm = conv(obj.YData,v,'same');
end
end
end
保存类文件后,您可以创建图表的实例。创建图表时,请省略分号以查看自定义显示。
x = 1:1:100;
y = 10*sin(x/15)+8*sin(10*x+0.5);
c = SmoothPlotCustomDisplay('XData',x,'YData',y)
c =
SmoothPlotCustomDisplay with properties:
SmoothColor: [0.9290 0.6940 0.1250]
XData: [1x100 double]
YData: [1x100 double]
Use GET to show all properties
![e18252dbb2c8856fd139c6a4e95b4c86.png](https://i-blog.csdnimg.cn/blog_migrate/d51aaf6a4b9ebc41d596aa3313dd29d4.png)
注:本文根据MATLAB官网内容修改而成。
我推荐给你以下的视频教程,特点是没有PPT,不掺水,直接编程环境下的实操课程: 用360分钟了解MATLAB编程《MATLAB编程360》视频课程:
MATLAB编程360 - 网易云课堂study.163.comstudy.163.com