Multiple custom graphics object inheriting from ChartContainer

조회 수: 7 (최근 30일)
Claudio
Claudio 2022년 4월 27일
댓글: Benjamin Kraus 2025년 6월 16일
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!

채택된 답변

Benjamin Kraus
Benjamin Kraus 2025년 2월 28일
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.
  댓글 수: 5
Fred Dilkes
Fred Dilkes 2025년 6월 14일
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?
Benjamin Kraus
Benjamin Kraus 2025년 6월 16일
@Fred Dilkes: I have sent that feedback to our documentation writer.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by