How do I get MATLAB to automatically account for missing XTick labels?

2 ビュー (過去 30 日間)
Brad
Brad 2014 年 9 月 30 日
コメント済み: Star Strider 2014 年 10 月 1 日
I've run into a scenario where MATLAB won't include the final xtick in some plots. For example, if I use the following code;
Time = [5 10 15 20 22.23];
Alt = [3 9 16 21 23];
plot(Time, Alt);
title('Altitude Vs Time');
set(gca, 'XLim', [0 max(Time)]);
xlabel('TALO (Sec)');
ylabel('Alt (Miles)');
I get a plot of altitudes vs time, where the desired x-axis tick values begin with 0, end with 20, at 5 second intervals. What I actually need is the same plot with X-axis ticks of 0, 5, 10, 15, 20, and 25.
Is there a way to get get MATLAB to automatically make the a-axis ticks extend past 20 to 25?

採用された回答

Star Strider
Star Strider 2014 年 9 月 30 日
Make one small change in the ‘set’ command in your original code, and the upper 'XLim' takes care of itself:
set(gca, 'XLim', [0 5*ceil(max(Time)/5)]);
This will automatically round the upper 'XLim' value to the next multiple of 5. Change the 5 values to something else if you want it to round to another value. Experiment with it.
  4 件のコメント
Brad
Brad 2014 年 9 月 30 日
Interesting.
I tested this approach on several data sets, some of which had multiples much larger than 5. I held the value of 5 constant, and it still ran like a champ, regardless of the data set. More testing is in order, but I'm a fan. Thanks for taking a look.
Star Strider
Star Strider 2014 年 10 月 1 日
My pleasure!

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

その他の回答 (0 件)

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by