Main Content

getAxes

Class: matlab.graphics.chartcontainer.ChartContainer
Namespace: matlab.graphics.chartcontainer

Get axes for chart container subclass

Syntax

ax = getAxes(obj)

Description

ax = getAxes(obj) returns one or more axes objects for a chart 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, or an array of axes objects. The contents of ax is useful for specifying the target axes when you call plotting functions within your class definition. You can also use ax to set properties on the axes.

Depending on the contents of the chart, ax might be a scalar axes object or an array of axes objects:

  • If the chart does not already contain an axes object, getAxes creates a Cartesian axes and returns it as ax.

  • If the chart contains one Cartesian, polar, or geographic axes object, ax is returned as that object.

  • If the chart contains multiple axes objects, ax is an array of those objects.

getAxes returns only Cartesian, polar, or geographic axes objects. It does not return other types of objects that are descendents of the TiledChartLayout.

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

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

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

Version History

Introduced in R2019b

expand all