How can I set xticklabels to an array?

5 views (last 30 days)
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];
for i=1:length(noise)
snr = [snr,10*log10((1000/noise(i))^2)];
% 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});
set(p5,'LineWidth',3, 'LineStyle', ':', 'MarkerSize', myMarkerSize,'Marker', 'x');
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)');
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.

Accepted Answer

DGM on 18 Nov 2022
Edited: DGM on 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.
xticklabels([Inf 2:10]) % numeric inf will be printed as "Inf"
Raghav Rathi
Raghav Rathi on 21 Nov 2022
Sorry for the confussion in my problem statement. The second approach you mentioned is what I was looking for. Thanks again.

Sign in to comment.

More Answers (0)




Community Treasure Hunt

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

Start Hunting!

Translated by