update

Class: matlab.graphics.chartcontainer.ChartContainer
Package: matlab.graphics.chartcontainer

Update instance of chart container subclass after setting properties

Syntax

update(obj)

Description

update(obj) updates the contents of the chart after one or more public property values change. Define this method to update the underlying graphics objects in the chart using the new property values. This method executes under the following conditions:

  • During the next drawnow execution, after the user changes one or more property values on the chart.

  • When an aspect of the user's graphics environment changes (such as the figure size).

Input Arguments

expand all

Object of the class that inherits from the matlab.graphics.chartcontainer.ChartContainer base class.

Attributes

Abstracttrue
Protectedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

Define a SmoothPlot class that plots a set of data using a dashed and dotted blue line with a smoothed version of the line.

To define the class, create a file named SmoothPlot.m that contains the following class definition with these features:

  • Four public properties: XData, YData, SmoothColor, and SmoothWidth

  • Two private properties: OriginalLine and SmoothLine

  • A setup method that initializes OriginalLine and SmoothLine

  • An update method that performs these tasks:

    • Updates the XData and YData properties of OriginalLine with the values stored in the XData and YData properties on the chart object

    • Updates the XData property of SmoothLine with the value stored in the XData property on the chart object

    • Calculates the value of the YData property of SmoothLine by calling the createSmoothData method.

    • Updates the Color and LineWidth properties of SmoothLine with the values stored in the SmoothColor and SmoothWidth properties on the chart object

  • A createSmoothData method that calculates a smoothed version of YData

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)
            % Get the axes
            ax = getAxes(obj);
            
            % Create the original and smooth lines
            obj.OriginalLine = plot(ax,obj.XData,obj.YData,'LineStyle',':');
            hold(ax,'on')
            obj.SmoothLine = plot(ax,NaN,NaN,'LineStyle','-',...
                'Color',obj.SmoothColor,'LineWidth',obj.SmoothWidth);
            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 vectors x and y. 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 = 10*sin(x./5)+8*sin(10.*x+0.5);
c = SmoothPlot('XData',x,'YData',y);

Use c to change the color of the smooth line to red.

c.SmoothColor = [1 0 0];

Introduced in R2019b