How to plot a datetime contained in a cell array
Mostrar comentarios más antiguos
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
Respuestas (1)
Peter Perkins
el 24 de Abr. de 2018
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.
Categorías
Más información sobre Dates and Time en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!