matlab.graphics.chartcontainer.ChartContainer.update - Update instance of chart container subclass after setting properties - MATLAB (original) (raw)
Main Content
Class: matlab.graphics.chartcontainer.ChartContainer
Namespace: matlab.graphics.chartcontainer
Update instance of chart container subclass after setting properties
Description
update([obj](#mw%5F21e659e0-7e34-463f-b8ec-a08539316624%5Fsep%5Fmw%5Fe72055e4-681f-4c08-a285-ed570d7c1140))
updates the contents of the chart after one or more property values change. Define this method to update the underlying graphics objects in the chart using the new property values. This method executes during the nextdrawnow
execution, after the user changes one or more property values on the chart.
Input Arguments
Object of the class that inherits from thematlab.graphics.chartcontainer.ChartContainer
base class.
Attributes
Abstract | true |
---|---|
Protected | true |
To learn about attributes of methods, seeMethod Attributes.
Examples
Define a class called SmoothPlot
that plots a set of data using a dotted blue line with a smoothed version of the line.
To define the class, create a file called SmoothPlot.m
that contains the following class definition with these features:
XData
andYData
public properties that store the _x_- and _y_-coordinate data for the original line.SmoothColor
andSmoothWidth
public properties that control the color and width of the smooth line.OriginalLine
andSmoothLine
private properties that store theLine
objects for original and smoothed data.- A
setup
method that initializesOriginalLine
andSmoothLine
. - An
update
method that updates the plot when the user changes the value of a property. - A
createSmoothData
method that calculates a smoothed version ofYData
.
classdef SmoothPlot < 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)
% Create 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);
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 sm = createSmoothData(obj)
% Calculate smoothed data
v = ones(1,10)*0.1;
sm = conv(obj.YData,v,'same');
end
end
end
Next, create a pair of x
and y
vectors. Plot x
and y
by calling the SmoothPlot
constructor method, which is provided by the ChartContainer
class. Specify the 'XData'
and 'YData'
name-value pair arguments and return the object as c
.
x = 1:1:100; y = 10sin(x./5) + 8sin(10.*x + 0.5); c = SmoothPlot('XData',x,'YData',y);
Use c
to change the color of the smooth line to red.
Tips
Avoid calling drawnow
within the setup
andupdate
methods of your chart class. Calling drawnow
in the setup
and update
methods can cause extraneous updates within your chart, leading to visual flickering and extraneous updates to objects outside your chart, which will negatively impact performance.
Version History
Introduced in R2019b