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.

Answers (2)

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 Comments

Im taking the dates specified above from Excel and wanting to paste them directly into MatLab. Your answer is telling me that I have to add 'quotes' to each cell I'm copying from Excel to create a cell array which takes more effort (call me lazy, so what). I don't want to use the import excel function and would prefer not to manually add the quotes in Excel before I copy the dates. There's not an easier way?
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.

Sign in to comment.

Walter Roberson
Walter Roberson on 23 May 2016
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.

Asked:

on 22 May 2016

Answered:

on 23 May 2016

Community Treasure Hunt

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

Start Hunting!