How to plot a datetime contained in a cell array
Ältere Kommentare anzeigen
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
Antworten (1)
Peter Perkins
am 24 Apr. 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.
Kategorien
Mehr zu Dates and Time finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!