Convert datenum to datetime for multiple holded plots

조회 수: 9 (최근 30일)
Jim Timon
Jim Timon 2019년 3월 31일
댓글: Cris LaPierre 2019년 12월 2일
My code:
h1 = plot(handles.x_datenum,handles.y,'.b');
hold on;
h2 = plot(ffitresult)
hold off;
Capture.JPG
X axis is datenum*10^9 because fit doesnt work if axis is datenum.
ffitresult = fit(handles.x_datenum.', handles.y, 'exp2');
I would like to see x axis in datetime format or in hours.
I tried this without success, it works only for one plot:
h1 = plot(handles.x_datenum,handles.y,'.b');
h1.XData = h1.XData/10^9
hold on;
h2 = plot(ffitresult)
h2.XData = h2.XData/10^9
%% Convert here datenum to datetime using datetick('x',dateFormat)
hold off;

답변 (1개)

Cris LaPierre
Cris LaPierre 2019년 4월 1일
It depends on your datenum source, but something like this should work.
t = datetime(731885.75,'ConvertFrom','datenum')
t =
datetime
31-Oct-2003 18:00:00
If your datenum is from Excel, use this instead
xlDate = 37925.75;
t = datetime(xlDate,'ConvertFrom','excel')
t =
31-Oct-2003 18:00:00
There are several datatypes supported by datetime. See this doc page for a description.
  댓글 수: 2
Om Gurav
Om Gurav 2019년 12월 2일
편집: Cris LaPierre 2019년 12월 2일
Hi,
I am having error in plotting time on x axis.
time = cdfread('file name', 'Variable', {'Timestamp'},'ConvertEpochToDatenum', true);
time1 = cell2mat(time);
time2=datetime(time1,'ConvertFrom','datenum');
after that i get date and time array like this,
'02-Feb-2017 00:00:00'
'02-Feb-2017 00:00:00'
'02-Feb-2017 00:00:00'
'02-Feb-2017 00:00:00'
'02-Feb-2017 00:00:00'
'02-Feb-2017 00:00:00'
'02-Feb-2017 00:00:00'
'02-Feb-2017 00:00:00'
'02-Feb-2017 00:00:00'
'02-Feb-2017 00:00:00'
Now I have to plot only time from this cell on x axis.
please help.
Cris LaPierre
Cris LaPierre 2019년 12월 2일
It would appear from your output there is no time information in your file (notice in the output that all times are 00:00:00). That, or your code is not capturing it. Therefore, all your data points are getting plotted at the same X-point
Perhaps look into the options for cdfread? Without seeing yoru data, it's hard to comment on what should be happening.
Also, it's always best to start your own question rather than asking in an existing one. It's more likely to be seen by others in the community instead of just the handful of us following this one, which increases the likelihood of getting a reply.

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

카테고리

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

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by