Plotting data with time series in matlab

回眸只為那壹抹淺笑 提交于 2019-12-04 05:26:24

问题


I have some data from 2007/5/1 to 2007/5/30 from 00:00 to 23:59:58. I want to plot these data according to data and time together. How can I define both date and time together? cause it has a regular date and time. For example

2007/5/1 00:00:00       -0.2
2007/5/1 00:00:02       -0.1
2007/5/1 00:00:04       -0.12
.
.
. 
2007/5/31 23:59:58      -0.4

I've been used DateTime code but I have regular time interval and I don't know how to solve it.


回答1:


Here is an example for using a datetime variable. You'll need to import your data to a corresponding vector that aligns with the time vector (t below) so that data(i) is the relevant data for t(i).

% create a datetime vector of all instances:
start = datetime('2007/5/1 00:00:00','InputFormat','uuuu/MM/dd HH:mm:ss');
step = duration(seconds(2));
fin = datetime('2007/5/31 23:59:58','InputFormat','uuuu/MM/dd HH:mm:ss');
t = start:step:fin; % a 1339200 elements vector, of all time steps
% some random data:
data = rand(numel(t),1);
% plotting samples 1 to 100:
plot(t(1:100),data(1:100))
xlim([datenum(t(1)) datenum(t(100))])

I use here random numbers for the example, and you won't be able to see anything for such a long vector, so I plot only a portion of it:



来源:https://stackoverflow.com/questions/41322696/plotting-data-with-time-series-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!