plotting Timeseries, setting xlim only to the range of data

12 次查看(过去 30 天)
while ploting a timeseries Matlab automatically takes xlim values beyond the actual data time limit. For example I have time start date and end as
beginTime='12-OCT-2012 14:00:00';
finishTime='12-OCT-2012 18:00:00'
but when I plot I get plot with xlim from '12-OCT-2012 13:55:12' to '12-OCT-2012 18:43:12'. How can I fix the xlim to exactly the time range of my data ?

回答(1 个)

per isakson
per isakson 2014-2-24
编辑:per isakson 2014-2-24
Warning: Not tested
date_frmt = 'dd-mmm-yyyy HH:MM:SS';
set(axes_handle,'XLim',[datenum(beginTime,date_frmt),datenum(finishTime,date_frmt)])
  2 个评论
Meh
Meh 2014-2-25
编辑:Meh 2014-2-25
Thanks Per, but I am getting strange dates on my plot. XLim is now from 18-Mar-4025 14:00:00 to 18-Mar-4025 18:00:00. Any suggestion? I used gca instead of axes_handle, but I don't think it plays any role, does it?
per isakson
per isakson 2014-2-28
编辑:per isakson 2014-2-28
I didn't pay enough attention to the difference between "timeseries" and "time series". The former is a Matlab class and the latter is a signal that depends on time. I've never used "timeseries" seriously.
With "time series", i.e. the old way, there is no problems:
date_frmt = 'dd-mmm-yyyy HH:MM:SS';
beginTime='12-OCT-2012 14:00:00';
finishTime='12-OCT-2012 18:00:00';
t = [ datenum(beginTime,date_frmt) : 1/(24*60) : ...
datenum(finishTime,date_frmt) ];
y = randn( size(t) );
plot( t, y )
axes_handle = gca;
datetick
set(axes_handle,'XLim', ...
[datenum(beginTime,date_frmt),datenum(finishTime,date_frmt)])
.
The class "timeseries" is a bit too smart to my taste.
  • Firstly, I cannot recreate the time axis you describe.
  • Secondly, I'm not sure whether datetick and/or set(h,'XLim'...) should be used - most likely not.
I tried
ts = timeseries( y, t );
ts.TimeInfo.Format = date_frmt;
ts.Time = ts.Time - ts.Time(1); % ????
figure
plot( ts )
Obviously, serial date numbers are not the default with timeseries.
>> ts.TimeInfo
tsdata.timemetadata
Package: tsdata
Non-Uniform Time:
Length 241
Time Range:
Start 0 seconds
End 1.666667e-01 seconds
Common Properties:
Units: 'seconds'
Format: 'dd-mmm-yyyy HH:MM:SS'
StartDate: ''
Most likely, the problems you see depend one the values of the properties, Start, Unit and/or StartDate.
"18-Mar-4025 14:00:00" is approx. two times 2013. Probably, a start value is "added" auto-magically to the XLim values of set(axes_handle,'XLim', ....
/over

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Time Series Events 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by