Change the format of an output figure of 'findchangepts'-function into datetime format.

5 views (last 30 days)
Hi!
When I use a function such as
findchangepts(data.val)
it automatically produces a figure. Unfortunately, the x-axis just shows sample numbers as it is not possible to, for example, use a timetable as input...
But I want the figure in datetime format. Right now, it is very inconvenient to change the figure into a dateformat:
[BPTipt, BPTresidual] = findchangepts(data.val) % my data gives me 2 change points
findchangepts(data.val)
h = get(gca, 'Children');
%% define datenums for Children's xdata:
t = datenum(data.Time(BPTipt));
t1 = [t(1) t(1) NaN t(2) t(2) NaN]; % get the format to set children xData
t2 = [datenum(data.Time(1)) t(1) NaN t(1) t(2) NaN t(2) datenum(data.Time(end)) NaN]; % get the format to set children xData
%% set new datenums as xdata for Children:
set(h(3,1),'xdata',datenum(data.Time));
set(h(2,1),'xdata',t2);
set(h(1,1),'xdata',t1);
datetick('x','mmm yy','keeplimits','keepticks');
and that way I still don't have datetime format...
My questions:
  • Is there an shorter easier way to get the output figure in a datetime format?
  • If not, how can i change a figure with datenums as x-data to a datetime format?
Thanks!

Accepted Answer

Seth Furman
Seth Furman on 28 Oct 2020
This same figure can be created with the relevant x data by using the plot, title, and xline commands.
load train
t = timetable(linspace(datetime(2020,1,1),datetime(2020,1,2),length(y))',y);
ipt = findchangepts(t.y,'MaxNumChanges',4,'Statistic','rms');
plot(t.Time,t.y)
title(sprintf('Number of changepoints = 4\nTotal log weighted dispersion = -33002.6521'));
xline(t.Time(ipt),'Color',[0.4660 0.6740 0.1880]);

More Answers (0)

Community Treasure Hunt

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

Start Hunting!