converting num to date on the xaxis in Matlab R2016a

Hello all! I am plotting a line plot with date on the x axis. The date is in numeric form eg 733590 and i want to convert it to dd -mmm- yy format. I tried using datetick('x','dd-mmm-yy') but it doesn`t plot the corresponding dates to the data. Instead, it plots from Jan whereas the data is in Sept.
sept_date=sept(:,1);
% sept_date1=datestr(sept_date)
% sept_date1=str2double(sept_date1)
% t = datetime(sept_date,'ConvertFrom','datenum');
coarse_values= sept(:,5);
figure
s=plot(sept_date,coarse_values, '-o');
datetick('x','dd mmm yy');

2 Comments

The attached plot is what i get. It should be september not january !
Don't mix datetime and datetick, it is eventually not going to work. You may be using an older version where under the hood, datetime plotting uses datenums, but that was only true for a few releases.

Sign in to comment.

Answers (2)

When MATLAB looked at the x data range it might have decided that the data most naturally rounded to a value that happened to be in January or December of the previous year. For example the date you gave 733590 happens to be in July 1, 2008. If your final date happened to be (say) 733773, Dec 31, 2008, then plausibly MATLAB might decide that the data most naturally rounded to 733400 to 733800, Dec 24, 2007 to Jan 27, 2009 because MATLAB prefers "round numbers" for the x boundaries.
You should consider using xlim() before the datetick() call.

6 Comments

thanks Walter, did you mean put xlim([733652 736941]) for example? That still gave the same plot as the start. Sorry my bad on the 733590, i was giving a random example. The real number are 733652 up to 736941. What is really boggling my mind is that i used it before and had no trouble. I`m just repeating the same plot with different tables (same type -double) and no loop.
As I had suspected, your data crosses several years and MATLAB is choosing the "round numbers" 733500 and 737000 as the limits. Then when you give the datetick, MATLAB is finding the nearest nice enclosing date boundaries, 733408 to 737061.
When I test, using xlim does make a difference to the plot. However, because your range of dates is sufficiently large, MATLAB is guessing that it should only put text labels every 2 years. If you need more labels then you can set the axes XTick property -- just make sure to set it the datenum of the dates you want as labels. Set the XTick before you call datetick, and pass the 'keepticks' property to datetick()
I`m sorry but can you show me in more detail. So you are saying matlab is rounding? I`m also plotting annual plots (in a loop and using datetick) are you saying i should double check those plots as Matlab would be rounding the dates?? Second, you are saying i should use xlim...should i write the xlimits of all the years that i have you mean? To be more clear...first, through a loop i plot my parameter every year and use datetick for the labeling. after the loop i plot data for only september of my 8 years. (here datetick did the error). Note this is a line plot.
Example:
x = 733652:736941;
y = rand(1, length(x));
mb = datenum( unique(dateshift(datetime(x,'ConvertFrom','datenum'),'start','month')) );
plot(x, y);
set(gca, 'XTick', mb(1:6:end)); %every 6 months
datetick('x', 'mm YYYY', 'keepticks')
Even with only labeling every 6 months, you will need a somewhat wide figure to prevent the labels from overlapping, so you might prefer
set(gca, 'XTick', mb(1:3:end), 'XTickLabelRotation', 90); %every 3 months, and rotated
datetick('x', 'mm YYYY', 'keepticks')
This code assumes R2013b or later for datetime, and assumes R2014b or later for XTickLabelRotation
thanks Walter for your help the x range is from 733652:736941; but is only 38 values..so its not every point within that range. So although the x axis is looking better, I`m still not there yet.
Set your x to your 38 values. Go through the mb calculation to find the unique months. set(gca, 'Xtick', mb, 'XTickLabelRotation', 90) and the datetick() and see how it looks.

Sign in to comment.

I think the problekm here is that plot typically puts ticks to show "nice" values in the range of your data, while you are looking for ticks at every single data point. If that is true, you are going to have to set the ticks. If you are using a recent version of MATLAB, try something like this:
d = datetime(2008:2018,9,1,'Format','dd-MMM-yy')
x = 1:length(d);
plot(d,x);
h = gca;
h.XTick = d;
If you are using a pre-R2016b version of MATLAB, you can do something similar with datenums and datetick, but don't mix datetick with datetime.

2 Comments

Thanks Peter...im getting the error that there is no XTick property on the Line class. im using Matlab R2016a
Not sure what you are doing and I have not dug through this thread so I may be answering the wrong question. If my guess is correct, that you want a tick for each data point and want to specify the format of the labels, I would think that this would do it:
d = datenum([733652 734017 734382 734747 735113 735478 735843 736208 736574 736939 737304]);
x = 1:length(d);
plot(d,x,'o');
h = gca;
h.XTick = d;
h.XTickLabel = datestr(d,'dd-mmm-yyyy');

Sign in to comment.

Categories

Asked:

on 25 Jan 2018

Commented:

on 30 Jan 2018

Community Treasure Hunt

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

Start Hunting!