How to add a datetime array to addpoints for an animated line plot?

조회 수: 5 (최근 30일)
Sun
Sun 2019년 10월 2일
댓글: Noah Prisament 2023년 6월 7일
Hi,
Let's say I have a datetime & temperature arrays as following:
Xdata Ydata
'05:00:00.048' 41
'05:00:00.052' 42
'05:00:00.056' 40.5
'05:00:00.060' 43
'05:00:00.064' 44
'05:00:00.068' 45
'05:00:00.072' 45.5
'05:00:00.076' 46
'05:00:00.080' 47
'05:00:00.084' 47.5
'05:00:00.088' 48
I would like to plot an animation line using addpoints. "addpoints" needs Xdata to be a double but I need to plot it as a datetime object.
Help me understand if there's another way around this.
Thanks,
Sun

채택된 답변

Walter Roberson
Walter Roberson 2019년 10월 2일
Unfortunately, animatedLine only permits double()
There are some obscure circumstances in plotting under which datetime objects will automatically be converted to doubles, but this is not one of those circumstances. :(
  댓글 수: 2
Eric Prandovsky
Eric Prandovsky 2023년 2월 9일
편집: Eric Prandovsky 2023년 2월 9일
Previous examples show converting to serial date number using datenum
datenum(datetime)
ans = 7.3893e+05
But the datenum documentation says it's no longer recommended, any idea what the new suggested method is?
Noah Prisament
Noah Prisament 2023년 6월 7일
The "animatedline" now supports "datetime" values natively, so this is the new suggested method. This will also show the proper ruler on each axis. See my answer below for example syntax.

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

추가 답변 (2개)

Ajay Kumar
Ajay Kumar 2019년 10월 2일
Try this:
Convert the datetime values to numbers using datenum.
Fetch these datenum values as x parameter to addpoints.
While plotting convert these datenum values to datetime again using datetime and 'ConvertFrom'.
I hope it works.
  댓글 수: 1
Sun
Sun 2019년 10월 2일
편집: Walter Roberson 2019년 10월 2일
Kumar,
Thanks for the response. addpoints itself plots the data. I am not able to do addpoints first and then convert the data for sake of a plot.
This is what I used:
h = animatedline('Marker','o');
addpoints(h,Xdata,Ydata);
This is the error: Error using matlab.graphics.animation.AnimatedLine/addpoints Invalid type for argument X. Type should be double.
I can try
Xdata_num = datenum(Xdata);
addpoints(h,Xdata_num,Ydata);
But plotting Xdata_num doesn't help as data constitutes to date which is the same for all time points.

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


Noah Prisament
Noah Prisament 2023년 6월 7일
The "animatedline" now supports all numeric datatypes as well as datetimes and durations for all axes as of R2023a!
In order to plot your data on an "animatedline" you can now utilize the following example:
h = animatedline(NaT, NaN);
addpoints(h, Xdata, Ydata);
or to initialize the "animatedline" with data:
h = animatedline(Xdata, Ydata);

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by