setting up Dates on x-axis

조회 수: 2 (최근 30일)
Tsvi Weiss
Tsvi Weiss 2017년 1월 9일
댓글: Walter Roberson 2017년 1월 20일
Hello I want to set up Dates on x-axis. This dates are taken from excel chart with value of in each date (about 96 different dates) and I want to plot at least 30 dates on x axis
This is what I got so far:
[Values,textdata] = xlsread('MinMaxVal');
length (Values);
figure(1);
plot(Values,'b');
hold on;
xx=textdata(2:length(textdata));
set(gca,'XTicklabel',xx);
set(gca,'XTickLabelRotation',45);
xlabel('Date');
ylabel('Value');
legend('Values','pksmax','minvalues',2)
grid on
Thanks

답변 (1개)

Peter Perkins
Peter Perkins 2017년 1월 12일
It's hard to tell what you are doing, but it looks like you're trying to make a tick mark for every value. That seems like a bad idea if you have more than a small number of values.
In MATLAB since R2014b, plot against time by create datetimes and just calling plot. If you want to have fine control over the ticks and labels, I think you'll need to set the tick locations as well as the labels. Prior to (IIRC) R2016a, controlling the ticks involved use of datenums as described in the doc. Since then, you can do it all "natively" using datetimes.
Also, I don't think you need hold on there. Hope this helps.
  댓글 수: 3
Peter Perkins
Peter Perkins 2017년 1월 19일
Walter, I think you are referring to the following:
Since R2014b, you can plot data against either a datetime or a duration, and the plot will automatically put up ticks with locations and formats that are usually appropriate. However, before R2016b, if you wanted to pick your own tick locations, or change the axis limits, you had to convert the datetime/duration into datenum units. The doc explained what to do.
But beginning in R2016b, everything is "native". So for example, in R2016b
>> t = hours(1:5); x = 1:5; plot(t,x)
>> xlim
ans =
1×2 duration array
0.9 hr 5.1 hr
>> xlim(hours([0 6]))
>> ax.XTick = hours(0:.5:6)
Walter Roberson
Walter Roberson 2017년 1월 20일
Yes, the situation did involve custom tick locations. When I was doing the debugging my mind got stuck in "fractions of day", and it wasn't until I replied to you that I realized that could be considered the same as datenum.

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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by