plotting Timeseries, setting xlim only to the range of data

21 ビュー (過去 30 日間)
Meh
Meh 2014 年 2 月 24 日
編集済み: per isakson 2014 年 2 月 28 日
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 Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by