I get date info when plot a Datetime in x-axis.

조회 수: 27 (최근 30일)
Victor Olivero
Victor Olivero 2021년 4월 2일
답변: Adam Danz 2023년 10월 18일
Hi, I am trying to plot a voltage data vs time data (HH:MN:SS) from a Datetime, but when I build the graph the x-axis always take the Year data (Dec 31, 1899).
I have tried modified the Datetime and the axis parameters, but always get this info.
How can I only display Hour:Minutes:Seconds? The year info is not important for me.
Thanks.
  댓글 수: 4
Victor Olivero
Victor Olivero 2021년 4월 4일
편집: dpb 2021년 4월 5일
My data come from a excel file, I import this to Matlab and after convert to a Datetime structure, then I get the hh:mm:ss but when I plot this data vs I_bateria or V_bateria, for example, the X-axis show the year data too, but it is a incorrect info (Dec 31, 1899).
Time I_bateria V_bateria T_bateria
0.4540 9.2688 13.1237 25.5063
0.4543 9.2659 13.1725 25.3310
0.4547 9.5520 13.1481 25.4186
0.4551 9.5424 13.1481 25.5063
0.4554 9.5155 13.0992 25.0684
0.4558 9.5288 13.0992 25.1559
0.4561 9.5123 13.0748 25.0684
dpb
dpb 2021년 4월 5일
Yes, that too. A MATLAB datetime doesn't exist without an associated date and time.
Unlike the venerable and deprecated datenum or an Excel date that are both represented as a julian date in a double where the whole portion of the value represents days and the non-integer part the fractional portion of a 24-hour day, the datetime is an object containing both a date and a time and the constructor for it will create a date from thin air if one isn't provided.
timeofday() does the equivalent conversion to a duration object as the alternative suggested -- the difference is the subtraction of the origin of the timeseries I suggested will give you a 0 origin irregardless of the actual time, timeofday will return the actual hours of the day of the datetime values.
Which is to use if not just the fixup of the display of the datae depends on situation.

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

채택된 답변

dpb
dpb 2021년 4월 2일
편집: dpb 2021년 4월 2일
Another case of TMW being too aggressive in hiding things from the end user inside graphics objects -- the year/date 'SecondaryLabel' property is hidden and is shown by default for all datetime axes.
Use
hAx=gca; % retrieve the current axes handle
hAx.XAxis.SecondaryLabel.Visible='off';
to make the year marker not be visible.
The alternative is to subtract the first time value of your datetime vector from all elements and convert to a duration and plot against it instead of the absolute time values.
dt=Time2-Time2(1); % convert to elapsed time; is a ML duration, not a datetime
plot(dt,v_bacteria)
  댓글 수: 1
Victor Olivero
Victor Olivero 2021년 4월 4일
Thanks for your reply, I have solved the issue taking, for example, plot(timeofday(Time2),V_bateria) using the timeofday fuction. However, I will try your recomends.

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

추가 답변 (1개)

Adam Danz
Adam Danz 2023년 10월 18일
Starting in MATLAB R2023b, use the xsecondarylabel function to set, change, or remove a secondary label.
For more info and a demo, see this Graphics and App Building blog article.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by