Can I change lines from numeric to datetime without deleting them?

조회 수: 1 (최근 30일)
Matt Butts
Matt Butts 2022년 9월 21일
댓글: Adam Danz 2022년 9월 22일
I have an already generated plot and I want to change the X data for all lines on the plot to some other data that happens to be datetimes (the original X values were numeric). When I do this, the lines on the primary Y seem to get converted ok, but when I have lines on a right Y, I am getting errors.
This simple script should demonstrate the issue:
% Create a figure
f = figure;
ax = axes(f);
% Initialize the right Y
yyaxis(ax,'right');
% Plot something on the left
yyaxis(ax,'left');
line1 = plot(ax,1:10,1:10);
% Plot something on the right
yyaxis(ax,'right')
line2 = plot(ax,1:10,(1:10).^2);
% Make the left Y active again
yyaxis(ax,'left');
% Change to datetime
% Some new x data
newX = datetime(2022,9,1:10);
% Set the XAxis ruler to be a datetimeruler
ax.XAxis = matlab.graphics.axis.decorator.DatetimeRuler;
drawnow;
line1.XData = newX;
line2.XData = newX;
Line 1 will update as expected, but Line 2 still wants numeric data for some reason. I have already tried setting the XAxis after switching the active Y axis as well and that doesn't seem to change anything.
Is there a better/different way to easily switch between numeric and datetime data for all lines on a plot?
I know I could delete the lines and run plot again, but the real use case has quite a few lines and the size of the data is pretty large so not having to redraw them would be ideal.
  댓글 수: 9
Matt Butts
Matt Butts 2022년 9월 22일
@Adam Danz This is part of an app that I am distributing to users and part of the app lets them choose from a bunch of channels in their data what they want to use for X and Y. The user may change their mind and want to select a different X.
Adam Danz
Adam Danz 2022년 9월 22일
If the options are mixed classes that require different axes types (e.g., numeric, categorical, datetime, duration), then it's better to just replace the line objects with new ones. Unless you're dealing with 100s of lines, the processing time won't differ noticeably between those two options.

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

답변 (0개)

카테고리

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

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by