Multiple custom graphics object inheriting from ChartContainer

8 views (last 30 days)
I am trying to write a custom chart (or graphical object, rather), and I would like to plot several of them in a single figure. I am following this guide, but I keep getting:
Error using matlab.graphics.chartcontainer.ChartContainer
Adding TestChart to axes is not supported. Turn hold off.
In my custom class I am combining a line and a surface, but this a minimal class works as an example:
classdef TestChart < matlab.graphics.chartcontainer.ChartContainer
properties (Access = private, Transient, NonCopyable)
% Chart axes.
HandleLine(1,1) % Handle of the line plot
end
properties
Axes
XData double {mustBeReal}
YData double {mustBeReal}
ZData double {mustBeReal}
end
methods (Access = protected)
function setup(this)
this.Axes = getAxes(this);
% Create line handle
this.HandleLine = plot3(this.Axes, NaN, NaN, NaN, '-o');
end
function update(this)
this.HandleLine.XData = this.XData;
this.HandleLine.YData = this.YData;
this.HandleLine.ZData = this.ZData;
end
end
And then, if I try creating two of them:
a = TestChart('XData', [0 0], 'YData', [0 0], 'ZData', [1 2]);
b = TestChart('XData', [0 0], 'YData', [0 0], 'ZData', [5 8]);
I get the error saying "Adding TestChart to axes is not supported. Turn hold off.".
Is it possible to create custom graphics object that can be plotted several times in the same axex ?
Thanks in advance!

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 28 Feb 2025
I can't reproduce this issue, but a couple things to keep in mind:
  1. Subclasses of ChartContainer are children of UI containers (like Figures, Panels, etc.). They are not children of axes (they are parents of axes). So, you can't have any charts in the axes, much less two or more.
  2. When you create a ChartContainer, it sets itself as the CurrentAxes (or gca).
  3. The default ChartContainer constructor will create a chart that replaces the current axes or chart (the thing returned by gca). So, if you call TestChart twice, the second one will replace the first one.
There are two workarounds to bullet 3:
The first is to create an axes in between the two calls:
a = TestChart('XData', [0 0], 'YData', [0 0], 'ZData', [1 2]);
axes % Create a new CurrentAxes
b = TestChart('XData', [0 0], 'YData', [0 0], 'ZData', [5 8]); % Will cause the axes to be deleted.
The second is to specify a position when you call the second chart:
a = TestChart('XData', [0 0], 'YData', [0 0], 'ZData', [1 2],'OuterPosition',[0 0 0.5 1]);
b = TestChart('XData', [0 0], 'YData', [0 0], 'ZData', [5 8],'OuterPosition',[0.5 0 0.5 1]);
The theory behind this behavior:
  • The default behavior is to replace the current chart or axes. This to aid in command-line noodling. Without this behavior, every time you called the command, you would get a new chart that overlaps the old chart. You (probably) wouldn't see the old chart, but it would still be there.
  • However, if you've specified a position, then you are more likely to be creating multiple charts and positioning them so they don't overlap. This will disable the "automatically replace the current chart/axes" behavior.
  4 Comments
Claudio
Claudio on 3 Mar 2025
Well, to be honest this post is from 3 years ago, and now I am not able to reproduce the issue of the graphics objects disappearing...
But I do understand your explanation of ChartContainer, I misunderstood its use !
Fred Dilkes
Fred Dilkes ongeveer 2 uur ago
Hello Benjamin,
I came across this behaviour in R2024a.
The following code is an extension of the SmoothPlot example from doc matlab.graphics.chartcontainer.ChartContainer.
f = figure;
ax = axes('Parent',f);
x = 1:1:100;
y = 10*sin(x./5) + 8*sin(10.*x + 0.5);
disp(ax)
c = SmoothPlot('XData',x,'YData',y...
,'Parent',f...
... ,'Position',[0 0 1 1]...
);
disp(ax)
As you suggest, if 'Position' is not specified then SmoothPlot seems to delete the pre-existing axes object.
This is very unexpected behavour that I have not been able to find documented elsewhere. May I suggest that this behaviour be emphasized in the official documentation?

Sign in to comment.

More Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!