Copy and paste date data
显示 更早的评论
How do I copy this date table to Matlab and create an array so that I can plot the dates vs. data?
5/1/2016 6/1/2016 7/1/2016 8/1/2016 9/1/2016 10/1/2016 11/1/2016 12/1/2016 1/1/2017 2/1/2017 3/1/2017 4/1/2017
Something so easy seems to be so hard! Ive tried (date being the table of dates above): Date = { date} Date = datenum({date}) Date = datetime({date}) Date = datestr(datenum({date}))
Ive tried everything I could find on Google with ‘no dice’. Help.
回答(2 个)
Star Strider
2016-5-22
I don’t know exactly what the problem is because I don’t have your actual data to work with.
Convert your dates to a cell array (if they aren’t already), then use datenum to convert them to date numbers, and datetick on the plot. (I got creative and rotated the x-tick labels to make them readable. That’s been an option for the last few releases.)
The code:
Dates = {'5/1/2016'
'6/1/2016'
'7/1/2016'
'8/1/2016'
'9/1/2016'
'10/1/2016'
'11/1/2016'
'12/1/2016'
'1/1/2017'
'2/1/2017'
'3/1/2017'
'4/1/2017'};
dnv = datenum(Dates);
y = exp((dnv - dnv(1)) * -0.01); % Create Data
figure(1)
plot(dnv, y)
datetick('x', 'yyyy-mm-dd', 'KeepTicks')
set(gca, 'XTickLabelRotation',30)
grid
2 个评论
Sonny Wilson
2016-5-23
Star Strider
2016-5-23
If you ask for the first two outputs from xlsread, you will find the dates as a cell array in the second output. You will not have to add any quotes, since the dates will be imported as a cell array, just as the one I created here. I added the quotes manually because I had to. You won’t.
This call to xlsread:
[d,s] = xlsread(...);
will have the dates in ‘s’. You should be able to use my code with it without modification.
Walter Roberson
2016-5-23
0 个投票
Use readtable in r2014b or later with datetime objects.
Otherwise the method depends upon whether you are using Windows or not, as xlsread on Windows can end up converting values that you don't want converted.
类别
在 帮助中心 和 File Exchange 中查找有关 Time Series Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!