how to put year as date tick in matlab

2 views (last 30 days)
hi, i am trying to put date tick in x axis using MATLAB 2010, date range is 2011/2012, but i have error in year format in x axis, the code is below thanks to help me
set(figure,'Color','white')
startDate = datenum(txt(w+2));
endDate = datenum(txt(end));
xData = linspace(startDate,endDate,length(de));
str = datestr(xData, 'mmm-yyyy');
plot(xData,de,'b',xData,re,'k')
NumTicks = 12;[enter image description here][1]
L = get(gca,'XLim');
set(gca,'XTick',linspace(L(1),L(2),NumTicks))
datetick('x','mmm yyyy','keeplimits', 'keepticks')
xticklabel_rotate;
set(gca,'XMinorTick','on','YMinorTick','on')
y_labels = get(gca, 'YTick');
set(gca, 'YTickLabel', y_labels); [year format error][1]
  5 Comments
samir hamid
samir hamid on 16 Feb 2016
Edited: Stephen23 on 17 Feb 2016
>> txt
txt =
'01/08/2008'
'02/08/2008'
'03/08/2008'
'04/08/2008'
'05/08/2008'
'06/08/2008'
'07/08/2008'
...
[elided very long list for brevity ... stephen]
dpb
dpb on 16 Feb 2016
There is no variable re in the above code snippet and the numbers pasted above seem to have no bearing on what the date string format is which is the issue if the date returned by datestr didn't match.
This is a two way street, we can't see your terminal from here so you've got to give sufficient detail and pertinent information to have any chance that somebody can solve the problem.
But, the general advice is, fix the call to datenum to include a date format string that matches what the input dates look like. When you've done that, check that the above test indicates you got it right.
doc datenum % see formatIn argument for details

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 16 Feb 2016
Change
startDate = datenum(txt(w+2));
endDate = datenum(txt(end));
to
startDate = datenum(txt(w+2), 'dd/mm/yyyy');
endDate = datenum(txt(end), 'dd/mm/yyyy');
  1 Comment
samir hamid
samir hamid on 17 Feb 2016
thanks to all who give me help, the Excel date format shold be changed, using x2mat function

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!