getAxes

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

Get axes for chart container subclass

Syntax

ax = getAxes(obj)

Description

ax = getAxes(obj) returns the current axes for a chart object that inherits from the matlab.graphics.chartcontainer.ChartContainer base class.

Input Arguments

expand all

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

Output Arguments

expand all

Axes object for the chart. Specify this object as the first input argument when you call a plotting function within your class definition. You can also use the object to set properties on the axes.

Attributes

Protectedtrue

To learn about attributes of methods, see Method Attributes.

Examples

expand all

The setup method is a common place to call plotting functions and set the axes hold state. In both cases, you must specify the target axes.

Create a setup method in your class definition file. Within that method, call getAxes to get the axes object ax. Then plot two lines by passing ax as the first argument to the plot and hold functions. Call hold(ax,'off') at the end of the method.

classdef TwoLinesPlot < matlab.graphics.chartcontainer.ChartContainer
    
    properties
        % ...
    end
    
    methods(Access = protected)
        function setup(obj)
            % Get the axes
            ax = getAxes(obj);
            
            % Plot two lines in the axes
            line1 = plot(ax,[1 2 3 4 5],[3 5 1 4 9]);
            hold(ax,'on')
            line2 = plot(ax,[1 2 3 4 5],[30 52 21 9 18]);
            
            % Turn off hold state
            hold(ax,'off')
        end
        function update(obj)
            % ...
        end
    end
end

Define a setup method in your class definition file. Within that method, call getAxes to get the axes object ax. Then set the x-axis color and the font angle for the axes. Call hold(ax,'on') before calling any plotting functions. Then call hold(ax,'off') at the end of the method.

classdef RedAxisPlot < matlab.graphics.chartcontainer.ChartContainer
    
    properties
        % ...
    end
    
    methods(Access = protected)
        function setup(obj)
            ax = getAxes(obj);
            ax.XColor = [1 0 0];
            ax.FontAngle = 'italic';
            hold(ax,'on')
            
            % Call plotting functions
            % ...
            
            hold(ax,'off')
        end
        function update(obj)
            % ...
        end
    end
end

Limitations

The getAxes method can return a Cartesian axes only. It cannot return other types of axes such as polar or geographic axes. Also, the axes provides limited support for setting the position and specifying the parent object.

  • Setting the OuterPositon, Position, ActivePositionProperty, or TightInset properties on the axes might produce unexpected results. Instead, configure the position on an instance of your chart.

  • Placing the axes into a tiled chart layout is not supported. However, you can place an instance of your chart into a tiled chart layout. For more information about tiled chart layouts, see tiledlayout.

  • Changing the Parent property of the axes is not recommended. Instead, specify the Parent property on an instance of your chart.

Introduced in R2019b