The attached plot is what i get. It should be september not january !
converting num to date on the xaxis in Matlab R2016a
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Peter Perkins
am 25 Jan. 2018
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.
Antworten (2)
Walter Roberson
am 25 Jan. 2018
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 Kommentare
Walter Roberson
am 30 Jan. 2018
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.
Peter Perkins
am 25 Jan. 2018
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 Kommentare
Peter Perkins
am 30 Jan. 2018
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');
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/187692/image.png)
Siehe auch
Kategorien
Mehr zu Title finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!