How can I set xticklabels to an array?

34 vues (au cours des 30 derniers jours)
Raghav Rathi
Raghav Rathi le 18 Nov 2022
Commenté : Raghav Rathi le 21 Nov 2022
Hi,
I am trying to generate a plot and I need to set the x axis and y axis lables to some value I am calculating.
Initially I was hard coding them as strings but its not really convenient as every time I tune any parameter, I have to redo them. Is there a better way to do so?
noise = [0,1000,3000,5000,7000,9000,11000,13000];
snr=[];
for i=1:length(noise)
snr = [snr,10*log10((1000/noise(i))^2)];
end
% Need to set xlim as the calculated values in snr.
Currently I am doing something like this
p1 = plot(obj1(1,:)/30);
hold on;
p2 = plot(obj1(4,:)/30);
p3 = plot(obj1(5,:)/30);
p4 = plot(obj1(6,:)/30);
p5 = plot(baseline/30);
phandles = [p1 p2 p3 p4];
for h=1:length(phandles)
set(phandles(h),'LineWidth',3, 'LineStyle', linestyles{h}, 'MarkerSize', myMarkerSize, 'Marker', markers{h});
end
set(p5,'LineWidth',3, 'LineStyle', ':', 'MarkerSize', myMarkerSize,'Marker', 'x');
grid;
xlabel('SNR (dB)')
ylabel('PRR of ACK A');
ylim([0 1.1]);
xticklabels({'Inf','0','-9.54','-13.97','-16.90','-19.08','-20.82','-22.27'}) %need to use an array instead
leg = legend({'x4', 'x2.77', 'x2.04', 'x1','Baseline-A'},'Location','northeast');
leg.FontSize = 30;
title(leg,'Power Ratio (A:B)');
set(gca,'FontSize',myFontSize);
a = get(gca,'XTickLabel');
set(gca,'XTickLabel',a,'fontsize',20,'FontWeight','bold') %changes the font size of axis lables
If someone can point me towards how I can use an array to set xticklabels or yticklabels then it would be really helpful.

Réponse acceptée

DGM
DGM le 18 Nov 2022
Modifié(e) : DGM le 18 Nov 2022
The array given to xticklabels() doesn't have to be a cell array of chars. It can be
  • a cell vector of chars
  • a cell vector of mixed char vectors and numeric scalars
  • a numeric vector
So if you can calculate those values, you could probably just use a numeric input instead of trying to convert to cellchar.
plot(1:10)
xticks(1:10)
xticklabels([Inf 2:10]) % numeric inf will be printed as "Inf"
  5 commentaires
DGM
DGM le 18 Nov 2022
Modifié(e) : DGM le 18 Nov 2022
Bear in mind that xticks corresponds to the xdata. In this case, that's implicitly the indices of obj2 (i.e. 1:15).
% Values to be plotted
obj2 = [
8 6 16 19 12 21 24 22 18 21 24 30 30 28 30
4 1 8 15 19 20 23 21 16 21 18 30 26 27 30
3 1 6 14 14 20 20 21 18 9 11 17 18 25 26
];
figure
p1 = plot(obj2(1,:)/30); hold on;
p2 = plot(obj2(2,:)/30);
p3 = plot(obj2(3,:)/30);
x_tick_lab = [0:-10:-120,-150,-200];
xticks(1:15); %Value must be a numeric vector whose values increase.
xticklabels(x_tick_lab); %label them as the original value
That said, I'm kind of confused with what you're trying to do with this relabeling. Using the labels alone to denote a nonlinear scale tends to be visually misleading, and I'm sure you can see how it requires a workflow that is prone to mistakes. If you're trying to force the axis direction to be reversed, you could accomplish that without needing to relabel everything.
% Values to be plotted
obj2 = [
8 6 16 19 12 21 24 22 18 21 24 30 30 28 30
4 1 8 15 19 20 23 21 16 21 18 30 26 27 30
3 1 6 14 14 20 20 21 18 9 11 17 18 25 26
];
x = [0:-10:-120,-150,-200];
figure
p1 = plot(x,obj2(1,:)/30); hold on;
p2 = plot(x,obj2(2,:)/30);
p3 = plot(x,obj2(3,:)/30);
set(gca,'xdir','reverse') % if you want to flip the axis direction
Granted, I just let this throw the ticks wherever it wanted. If you do want all the ticks, you'd need to specify that, but the labels wouldn't need to be specified.
% Values to be plotted
obj2 = [
8 6 16 19 12 21 24 22 18 21 24 30 30 28 30
4 1 8 15 19 20 23 21 16 21 18 30 26 27 30
3 1 6 14 14 20 20 21 18 9 11 17 18 25 26
];
x = [0:-10:-120,-150,-200];
figure
p1 = plot(x,obj2(1,:)/30); hold on;
p2 = plot(x,obj2(2,:)/30);
p3 = plot(x,obj2(3,:)/30);
xticks(fliplr(x))
set(gca,'xdir','reverse') % if you want to flip the axis direction
Raghav Rathi
Raghav Rathi le 21 Nov 2022
Sorry for the confussion in my problem statement. The second approach you mentioned is what I was looking for. Thanks again.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Signal Generation, Manipulation, and Analysis dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by