converting num to date on the xaxis in Matlab R2016a

4 ビュー (過去 30 日間)
Yasmin Samy
Yasmin Samy 2018 年 1 月 25 日
コメント済み: Peter Perkins 2018 年 1 月 30 日
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 件のコメント
Yasmin Samy
Yasmin Samy 2018 年 1 月 25 日
The attached plot is what i get. It should be september not january !
Peter Perkins
Peter Perkins 2018 年 1 月 25 日
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.

サインインしてコメントする。

回答 (2 件)

Walter Roberson
Walter Roberson 2018 年 1 月 25 日
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 件のコメント
Yasmin Samy
Yasmin Samy 2018 年 1 月 30 日
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.
Walter Roberson
Walter Roberson 2018 年 1 月 30 日
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
Peter Perkins 2018 年 1 月 25 日
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 件のコメント
Yasmin Samy
Yasmin Samy 2018 年 1 月 25 日
Thanks Peter...im getting the error that there is no XTick property on the Line class. im using Matlab R2016a
Peter Perkins
Peter Perkins 2018 年 1 月 30 日
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');

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTitle についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by