How to plot Date time series in MATLAB?

조회 수: 20 (최근 30일)
Ali
Ali 2019년 2월 15일
댓글: Peter Perkins 2019년 2월 21일
I am facing difficulties in plotting my time series data.The data file is also attached.I write code like this but doesnt work.
data3=readtable ('PRICE_AND_DEMAND_Jan.csv')
Date_Time = data3(:,2);
datecell=table2cell(Date_Time);
load = data3(:,3);
A = table2array(load)
p=datenum(datecell);
plot(datenum(datecell), A)
datetick('x', 'dd-mmm-yyyy HH:MM:SS')
% datetick('x', 'dd-mmm-yyyy HH:MM:SS')
  댓글 수: 3
Ali
Ali 2019년 2월 16일
Matlab 2018b.
I think MATLAB in my computer has changed the default settings of date reading.It by deafault reads dates in YYYY-MM-DD format.
Seriously,this plotting problem is irritating now.
Peter Perkins
Peter Perkins 2019년 2월 17일
You have dates like xx-yy-zz. That's kind of ambiguous. You'll want to provide a format.

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

채택된 답변

Ricardo MF
Ricardo MF 2019년 2월 15일
Try it:
data3=readtable ('PRICE_AND_DEMAND_Jan.csv')
Date_Time = data3(:,2);
datecell=table2cell(Date_Time);
load = data3(:,3);
A = table2array(load)
formatIn='dd-mm-yy HH:MM'
p=datenum(datecell,formatIn);
plot(p, A)
datetick('x', 'dd-mmm-yyyy HH:MM')
set(gca,'XTickLabel',datestr(p),'XMinorGrid','on')
grid
  댓글 수: 5
Ricardo MF
Ricardo MF 2019년 2월 15일
Still with a problem to 'calibrate' the x axes but the code 'works'.
clear all
close all
data3=readtable ('PRICE_AND_DEMAND_Jan.csv')
Date_Time = data3(:,2);
datecell=table2cell(Date_Time);
load = data3(:,3);
A = table2array(load)
formatIn='dd-mm-yy HH:MM'
p=datenum(datevec(datecell,formatIn));
plot(p,A)
datetick('x', 'dd-mmm-yyyy HH:MM')
set(gca,'XTick',p([1 size(p,1)/4 size(p,1)/2 (size(p,1)/4)*3 end]),'XTickLabel',datestr(p),'XMinorGrid','on','FontSize',7)
grid
Ali
Ali 2019년 2월 15일
Still i am getting error due to formatting in Date and time as shown in attached image.Can you pls check the formatting of date and time.There is something wrong there.error 2.png

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

추가 답변 (2개)

Ricardo MF
Ricardo MF 2019년 2월 15일
Are you running the entire code or assuming you already 'uploaded' the variables? The problem is with the format of your variable Date_Time that for some reason converted to a 'char' vector. Instead mine's not. When I convert to datecell, I do not have problems with datenum or datevec. Maybe this problem is related to the version of matlab as questioned by madhan ravi.
MA2.png
  댓글 수: 1
Ali
Ali 2019년 2월 16일
I am running the entire code you posted above.I think my PC Matlab is reading the dates incorrectly.Is there any command to reset the date preferences in MATLAB?

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


Peter Perkins
Peter Perkins 2019년 2월 17일
In something as recent as R2018b, stay away from datenum and datetick if possible. (Also, hardly ever does one need table2cell, brace subscripting would have done the same thing, but you don't want to do that thing anyway.)
Try this:
>> t = readtable('PRICE_AND_DEMAND_Jan.csv','Format','%s%{dd-MM-yy HH:mm}D%f%f%s');
>> t.REGION = categorical(t.REGION);
>> t.PERIODTYPE = categorical(t.PERIODTYPE);
>> head(t)
ans =
8×5 table
REGION SETTLEMENTDATE TOTALDEMAND RRP PERIODTYPE
______ ______________ ___________ _____ __________
NSW1 01-01-19 00:30 7457.6 66.95 TRADE
NSW1 01-01-19 01:00 7243.2 69.33 TRADE
NSW1 01-01-19 01:30 6918.6 72.8 TRADE
NSW1 01-01-19 02:00 6676.6 70.31 TRADE
NSW1 01-01-19 02:30 6513.2 66.98 TRADE
NSW1 01-01-19 03:00 6400 63.87 TRADE
NSW1 01-01-19 03:30 6317.4 62.2 TRADE
NSW1 01-01-19 04:00 6242.1 50.22 TRADE
>> plot(t.SETTLEMENTDATE,t.TOTALDEMAND);
untitled.png
  댓글 수: 4
Ali
Ali 2019년 2월 18일
편집: Ali 2019년 2월 18일
Thank you peter for your useful reply.How to set time and date limits on x-axis? i tried to use "xlim" but it give me errors.for instance:
start time =01-jan-2016 00:00
end date =01-jan-2016 23:30
Peter Perkins
Peter Perkins 2019년 2월 21일
You are going to need to post something more than that. That is not MATLAB code. In recent versions of MATLAB, all you need to do it set xlim with datetimes.

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

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by