How to plot a datetime contained in a cell array

I have two variables from an imported excel dataset, 'Price' and 'Date', which the second is a cell array that contains a sequence of datetime variables. When I try to plot in a figure the two variables, I have problem with 'Date' because the type of the variable is un Invalid Input, hence I can't plot it. How can I solve this problem about 'Date'? This is the code with the error in the last line.
if
[P,~] = xlsread('dataset-2018-04-18-18-02-05.xlsx','Stock Prices','B3:SJ3275');
[~,Date] = xlsread('dataset-2018-04-18-18-02-05.xlsx','Stock Prices','A3:A3275');
P = flipud(P);
Date = flipud(Date);
plot(Date,P,'b'); % Error: 'Invalid first data argument'
end

답변 (1개)

Peter Perkins
Peter Perkins 2018년 4월 24일
Walter, your code won't create datetime variabels at all. What it may create is a cell array of text timestamps.
In recent versions of MATLAB, you will probably be happier using readtable, which will automatically create a datetime variable in the table (assuming the spreadsheet is formatted reasonably). Then it's just
plot(t.Date,t.Price)
or something similar. If you stick with xlsread, you're gonna need to convert your cell array to datetimes. Probably you just call datetime on it.

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

2018년 4월 23일

답변:

2018년 4월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by