MATLAB Answers

ChartContainer is broken?

6 views (last 30 days)
Jan Kappen
Jan Kappen on 9 Feb 2021
Commented: Jan Kappen on 16 Feb 2021
Hi all,
Since R2020a, class properties, provided by the class constructor are assigned after the setup method runs, see
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 <
Data timetable
properties (Access = private)
NumSubplots = 1
function obj = BrokenChartClass(T)
T timetable
obj.NumSubplots = length(T.Properties.VariableNames); % create as many subplots as columns in data table
obj.Data = T;
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);
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})
%% test via
T = timetable((datetime():seconds(1):datetime+hours(1))',randn(3601,1),10*randn(3601,1)); % 3601 rows, 2 data cols
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?
  1 Comment
Jan Kappen
Jan Kappen on 16 Feb 2021
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?!

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by