ChartContainer is broken?

조회 수: 4 (최근 30일)
Jan Kappen
Jan Kappen 2021년 2월 9일
댓글: Jörn Froböse 2022년 10월 16일
Hi all,
Since R2020a, class properties, provided by the class constructor are assigned after the setup method runs, see https://www.mathworks.com/help/matlab/ref/matlab.graphics.chartcontainer.chartcontainer.setup.html#mw_892e1871-5986-41cf-b037-396fa9a9adbf
That means, I can't create plots that rely somehow on external information, provided by the user via constructor. An easy example would be this:
The user provides a timetable. Each column should be plotted to its own subplot:
classdef BrokenChartClass < matlab.graphics.chartcontainer.ChartContainer
properties
Data timetable
end
properties (Access = private)
NumSubplots = 1
DataAxes
DataLines
end
methods
function obj = BrokenChartClass(T)
arguments
T timetable
end
obj@matlab.graphics.chartcontainer.ChartContainer();
obj.NumSubplots = length(T.Properties.VariableNames); % create as many subplots as columns in data table
obj.Data = T;
end
end
methods (Access = protected)
function setup(obj)
tcl = getLayout(obj);
obj.DataAxes = arrayfun(@(n)nexttile(tcl, n), 1:obj.NumSubplots);
obj.DataLines = arrayfun(@(hax)plot(hax, NaT, NaN), obj.DataAxes);
end
function update(obj)
% Extract the time data from the table.
tbl = obj.Data;
t = tbl.Properties.RowTimes;
for k = 1:length(obj.DataLines)
set(obj.DataLines(k), 'XData', t, 'YData', tbl{:,k})
end
end
end
end
%% test via
T = timetable((datetime():seconds(1):datetime+hours(1))',randn(3601,1),10*randn(3601,1)); % 3601 rows, 2 data cols
head(T)
BrokenChartClass(T(:,1)); % -> one subplot, makes sense
BrokenChartClass(T(:,1:2)); % -> one subplot (uses default value of NumSubplots), not as expected but documented
Could you please help me understanding this concept? Calling setup before assigning the properties makes it impossible to use it. I always have to create another mySetup function to be called in update which requires some flag like bool IsInitialized or so.
I can't believe that's the purpose of this class. What am I missing?
Thanks!
  댓글 수: 2
Jan Kappen
Jan Kappen 2021년 2월 16일
Addendum, suppose you want to embed the chart class into a panel or uifigure, you can specify the Parent property. But since it's not assigned in setup(), a default figure is opened when I call getLayout(). I'd need to remember the old parent and overwrite it in a second call to setup().
This renders the automatic call of setup completely useless. Did I get something wrong?!
Jörn Froböse
Jörn Froböse 2022년 10월 16일
Same thing for me. Did you find any better solution than this workaround with mySetup?

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Developing Chart Classes에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by