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)

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Grid 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