How to display a few xticklabels from a set of selected xticks?

1 回表示 (過去 30 日間)
Sim
Sim 2023 年 10 月 31 日
コメント済み: Voss 2023 年 10 月 31 日
How to display a few xticklabels, like [4, 53] in the following example, from a set of selected xticks?
selected_ticks = [2 3 4 5 6 8 11 1 19 29 53 119];
sorted_selected_ticks = sort(selected_ticks);
plot(1:120,1:120)
xticks(sorted_selected_ticks)
Indeed, just by adding xticklabels([4 53]) after xticks(sorted_selected_ticks) I do not get what expected, i.e. tick lables in position 4 and position 53, and labelled as 4 and 53, respectively, as in the following figure (which is my desired output):

採用された回答

Voss
Voss 2023 年 10 月 31 日
selected_ticks = [2 3 4 5 6 8 11 1 19 29 53 119];
selected_labels = [4 53];
sorted_selected_ticks = sort(selected_ticks);
sorted_selected_labels = sort(selected_labels);
[ism,idx] = ismember(sorted_selected_labels,sorted_selected_ticks);
assert(all(ism))
xlabels = strings(1,numel(sorted_selected_ticks));
xlabels(idx) = string(sorted_selected_labels);
plot(1:120,1:120)
xticks(sorted_selected_ticks)
xticklabels(xlabels)
  2 件のコメント
Sim
Sim 2023 年 10 月 31 日
Many many thanks @Voss!! :-)
Voss
Voss 2023 年 10 月 31 日
You're welcome!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGrid Lines, Tick Values, and Labels についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by