setting xaxis labels to only integers
Afficher commentaires plus anciens
Hello,
I have a plot where I am trying to make it so that the xaxis is going to be integers and steps of 1.
figure();
adcValues_flipped = flip(adcValues);
xlabels_flipped = flip(num2str(x_mm_flipped));
b0_flipped = flip(b0Values);
plot(1:numel(x_mm), adcValues_flipped);
set(gca, 'XTick', 1:numel(x_mm), 'XTickLabel', xlabels_flipped);
Right now the code about gives me an xaxis labels of the following:
'-4.95'
' -4.5'
'-4.05'
' -3.6'
'-3.15'
' -2.7'
'-2.25'
' -1.8'
'-1.35'
' -0.9'
'-0.45'
' 0'
' 0'
' 0'
' 0'
' 0'
' 0'
' 0.45'
' 0.9'
' 1.35'
' 1.8'
' 2.25'
' 2.7'
' 3.15'
' 3.6'
' 4.05'
' 4.5'
' 4.95'
' 5.4'
' 5.85'
Where I would instead like it to be -5, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 as the xaxis labels and ticks.
Any ideas?
Réponse acceptée
Plus de réponses (1)
x_mm = [-4.95 -4.5 -4.05 -3.6 -3.15 -2.7 -2.25 -1.8 -1.35 -0.9 -0.45 0 0 0 0 0 0 0.45 0.9 1.35 1.8 2.25 2.7 3.15 3.6 4.05 4.5 4.95 5.4 5.85]';
% round x_mm to get to integers
x_mm_round = round(x_mm);
% get the absolute of the difference between x_mm and x_round. It should be
% below a vertein value (in this case 0.2) for all the values that are
% closest to or are a certain integer
idx = abs(x_mm-x_mm_round) < 0.2;
% remove the ticks that you are not interested in
fig_xticks = 1:numel(x_mm);
fig_xticks = fig_xticks(idx);
% create the corresponding xticklables
fig_xticklabels = num2str(x_mm);
% you could insert x_mm_round here instead to get the integers,
% but I would not recomment this, since it creates untruthfull labeling of the data
% if you would like to use integers here you would need to recalculate the tick positions
fig_xticklabels = fig_xticklabels(idx,:);
figure; axes; xlim([1 numel(x_mm)])
set(gca,'XTick',fig_xticks,'XTickLabel',fig_xticklabels,'XTickLabelRotation',90)
2 commentaires
Alternativly, if you would like to keep the ticks ,you can use:
x_mm = [-4.95 -4.5 -4.05 -3.6 -3.15 -2.7 -2.25 -1.8 -1.35 -0.9 -0.45 0 0 0 0 0 0 0.45 0.9 1.35 1.8 2.25 2.7 3.15 3.6 4.05 4.5 4.95 5.4 5.85]';
x_mm_round = round(x_mm);
idx = abs(x_mm-x_mm_round) < 0.2;
fig_xticks = 1:numel(x_mm);
fig_xticklabels = num2str(x_mm);
fig_xticklabels(~idx,:) = ' ';
figure; axes; xlim([1 numel(x_mm)])
set(gca,'XTick',fig_xticks,'XTickLabel',fig_xticklabels,'XTickLabelRotation',90)
nina
le 12 Juin 2025
Catégories
En savoir plus sur Axis Labels dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!







