Plot time series data

조회 수: 3 (최근 30일)
Nurfaiz Fathurrahman
Nurfaiz Fathurrahman 2022년 8월 30일
답변: Lei Hou 2022년 8월 31일
first, sorry for my english
i have the data below
2022-08-27T00:00:00.019538 16065
2022-08-27T00:00:00.044538 16871
2022-08-27T00:00:00.069538 16800
2022-08-27T00:00:00.094538 16574
2022-08-27T00:00:00.119538 17022
2022-08-27T00:00:00.144538 17021
2022-08-27T00:00:00.169538 16746
2022-08-27T00:00:00.194538 16948
first column is date and time, second column is data
I want to make a timeseries plot, I've tried with
ts = timeseries(data, time)
and then i get error
Error using datenum (line 189)
DATENUM failed.
Error in timeseries/init (line 318)
[~, data, quality, I] = timeseries.utsorttime(datenum(time),...
Error in timeseries (line 343)
this = init(this,varargin{:});
Caused by:
Error using datevec (line 217)
Failed to lookup month of year.
how to make timeseries from data above? thank you
  댓글 수: 2
Walter Roberson
Walter Roberson 2022년 8월 30일
Are you sure you need timeseries()? Would it be plausible to instead use the newer timetable() ?
Nurfaiz Fathurrahman
Nurfaiz Fathurrahman 2022년 8월 30일
I'm not sure, I need to plot the above data to get a graph like this
is it possible to use timetable()? Or is there another method besides these two methods?

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

답변 (2개)

Star Strider
Star Strider 2022년 8월 30일
편집: Star Strider 2022년 8월 30일
Try something like this —
C = {'2022-08-27T00:00:00.019538' 16065
'2022-08-27T00:00:00.044538' 16871
'2022-08-27T00:00:00.069538' 16800
'2022-08-27T00:00:00.094538' 16574
'2022-08-27T00:00:00.119538' 17022
'2022-08-27T00:00:00.144538' 17021
'2022-08-27T00:00:00.169538' 16746
'2022-08-27T00:00:00.194538' 16948};
DT = datetime(C(:,1), 'InputFormat','yyyy-MM-dd''T''HH:mm:ss.SSSSSS', 'Format','yyyy-MM-dd HH:mm:ss.SSSSSS')
DT = 8×1 datetime array
2022-08-27 00:00:00.019538 2022-08-27 00:00:00.044538 2022-08-27 00:00:00.069538 2022-08-27 00:00:00.094538 2022-08-27 00:00:00.119538 2022-08-27 00:00:00.144538 2022-08-27 00:00:00.169538 2022-08-27 00:00:00.194538
T1 = table(DT, cell2mat(C(:,2)))
T1 = 8×2 table
DT Var2 __________________________ _____ 2022-08-27 00:00:00.019538 16065 2022-08-27 00:00:00.044538 16871 2022-08-27 00:00:00.069538 16800 2022-08-27 00:00:00.094538 16574 2022-08-27 00:00:00.119538 17022 2022-08-27 00:00:00.144538 17021 2022-08-27 00:00:00.169538 16746 2022-08-27 00:00:00.194538 16948
figure
plot(T1{:,1}, T1{:,2})
grid
xtickformat('mm:ss.SSS')
xtickangle(30)
If your data are in a file, use readtable and then do the appropriate datetime conversion as I outlined here.
EDIT — Corrected typographical errors, added xtickformat and xtickangle calls. .
.

Lei Hou
Lei Hou 2022년 8월 31일
The function 'stackedplot' is designed for plotting time series.
>> C = {'2022-08-27T00:00:00.019538' 16065
'2022-08-27T00:00:00.044538' 16871
'2022-08-27T00:00:00.069538' 16800
'2022-08-27T00:00:00.094538' 16574
'2022-08-27T00:00:00.119538' 17022
'2022-08-27T00:00:00.144538' 17021
'2022-08-27T00:00:00.169538' 16746
'2022-08-27T00:00:00.194538' 16948};
>> DT = datetime(C(:,1), 'InputFormat','yyyy-MM-dd''T''HH:mm:ss.SSSSSS', 'Format','yyyy-MM-dd HH:mm:ss.SSSSSS');
>> numericValue = cell2mat(C(:,2));
>> tt = timetable(numericValue,'RowTimes', DT)
tt =
8×1 timetable
Time numericValue
__________________________ ____________
2022-08-27 00:00:00.019538 16065
2022-08-27 00:00:00.044538 16871
2022-08-27 00:00:00.069538 16800
2022-08-27 00:00:00.094538 16574
2022-08-27 00:00:00.119538 17022
2022-08-27 00:00:00.144538 17021
2022-08-27 00:00:00.169538 16746
2022-08-27 00:00:00.194538 16948
>> stackedplot(tt)

카테고리

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