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
Star Strider 2016년 5월 22일

0 개 추천

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
Sonny Wilson 2016년 5월 23일
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.

댓글을 달려면 로그인하십시오.

Walter Roberson
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에 대해 자세히 알아보기

태그

질문:

2016년 5월 22일

답변:

2016년 5월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by