how to plot datetime against number

조회 수: 4 (최근 30일)
rohail khan
rohail khan 2018년 4월 10일
답변: Peter Perkins 2018년 4월 11일
hi, I imported some data from excel file.I have two columns.One is dates and other is stocks. 'dates' column is of the form 'datetime' and 'stocks' is of the form 'double'. I just want to plot(dates,stocks) but it wont work. How can I plot dates with stocks or how to convert the dates to numeric?

채택된 답변

KSSV
KSSV 2018년 4월 10일
You can plot class of datetime and double in MATLAB. Read the excel file using xlsread. Convert the columns of dates into class datetime using datetime.
t = datetime(2014,6,28) + calweeks(0:9);
y = rand(1,10);
plot(t,y);
Read about datetime.
  댓글 수: 2
rohail khan
rohail khan 2018년 4월 10일
thanks KSSV.I read the datetime page but couldnt understand well.I mean how can I convert a complete Column vector of dates to numbers(Note: dates are in the form 2/02/2018)
KSSV
KSSV 2018년 4월 10일
str = '2/02/2018'
datetime(str)

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

추가 답변 (1개)

Peter Perkins
Peter Perkins 2018년 4월 11일
rohail, you don't need to convert anything if your data are already datetimes, which seems to be what you are indicating. Ideally, in recent releases of MATLAB and assuming your spreadsheet is properly formatted, all you need is
t = readtable('myfile.xlsx');
plot(t.Dates,t.Stocks)
readtable may end up returning you text for the dates if the spreadsheet is not formatted with date cells, in that case just convert to datetime as KSSV suggests.

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by