string for x label in a plot

Hello,
I have 15 values for x label but they are not consecutive values. for exampl they are bitrate values from 100 kb to 900 kb and 1 mb to 6 mb. but I do not know how can I put them in x axis. I have to put
x=1:15
to have 15 values in x axis. another problem is in x axis we only have numbers such as 0, 5,10 and 15 but I want to have all values in x axis. could you please help me with this issue:? my realese is R2016a. thanks.

回答 (4 件)

DGM
DGM 2022 年 5 月 31 日

2 投票

You can make the tick labels whatever you want. They don't have to be a literal label of the tick locations.
x = 1:10;
y = sin(x);
xtlbls = {'cat' 'dog' 'tree' 'fruit' '10.413' '⅄∃H' 'red' 'blue' 'words' 'numbers'};
plot(x,y);
xticks(x) % define tick locations explicitly
xticklabels(xtlbls) % define tick labels

4 件のコメント

nadia naji
nadia naji 2022 年 5 月 31 日
I am a beginner in Matlab and I am not familiar with xticks. how can I define it?
Walter Roberson
Walter Roberson 2022 年 5 月 31 日
https://www.mathworks.com/help/matlab/ref/xticks.html
Star Strider
Star Strider 2022 年 5 月 31 日
The xticks function was introduced in R2016b.
DGM
DGM 2022 年 5 月 31 日
Oof. I knew it was R2016b, but I misread the version OP was using.
You can always use set() to set the 'xtick' and 'xticklabel' property of the current axes.

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

Star Strider
Star Strider 2022 年 5 月 31 日
編集済み: Star Strider 2022 年 5 月 31 日

1 投票

Try this —
figure
plot((1:15), sin((1:15)/2.5))
xtl = [(1:9)*1E8 (1:6)*1E9];
set(gca, 'XTick',(1:15), 'XTickLabel',xtl)
EDIT — (31 May 2022 at 15:11)
Added x-tick labels to match units in the original post.
EDIT — (31 May 2022 at 18:08)
Another option for the labels:
xtl = [sprintfc('%3d kb',(1:9)*100) sprintfc('%d mb',1:6)]
figure
plot((1:15), sin((1:15)/2.5))
xtl = [sprintfc('%3d kb',(1:9)*100) sprintfc('%d mb',1:6)];
set(gca, 'XTick',(1:15), 'XTickLabel',xtl)
.
Jan
Jan 2022 年 5 月 31 日
編集済み: Jan 2022 年 5 月 31 日

1 投票

It is not really clear, what you are asking for. But you can set the XTicks and the corresponding lables freely.
x = 1:15;
y = rand(1, 15);
axes('XTick', 1:15, 'XLim', [1, 15], 'XTickLabel', ...
{'A', 'b', '17', '5', 'D', 'now', 'then', '9', '-pi', '10', ...
'11', '12', '-', '14', 'ready'}, ...
'NextPlot', 'add');
plot(x, y);
Walter Roberson
Walter Roberson 2022 年 5 月 31 日

1 投票

xticks(1:15)
Lab = [(1:9) + "kb", (1:6) + "mb"]
xticklabels(Lab)
Are you sure that you want linear 1:15 x, but 1:9 vs 10:15 is at different scales? The entry for x=11 is not 2 units more than the entry for x=9, and that is going to lead to misleading plots.

4 件のコメント

nadia naji
nadia naji 2022 年 5 月 31 日
what is xticks, becuase it produces this error.
Undefined function or variable 'xticks'
nadia naji
nadia naji 2022 年 5 月 31 日
the value of them is not important and they are not related to each other.
Walter Roberson
Walter Roberson 2022 年 5 月 31 日
編集済み: Walter Roberson 2022 年 5 月 31 日
xticks as a function was introduced in the release immediately after the one you have. In your release
set(gca, 'Xtick', 1:15)
set(gca, 'XTickLabel', Lab)
nadia naji
nadia naji 2022 年 5 月 31 日
in my matlab release we do not have "" and I changed it to
Lab = [(1:9) + 'kb', (1:6) + 'mb']
but it produces this error:
Matrix dimensions must agree
my code is :
Lab = [(1:9) + 'kb', (1:6) + 'mb']
set(gca, 'Xtick', 1:15)
set(gca, 'XTickLabel', Lab)
h = zeros(6,4);
h(:,1) = plot(x,res540,'-r');
hold on
h(:,2) = plot(x,res720,'--b');
h(:,3) = plot(x,res900,'-.g');
h(:,4) = plot(x,res1080,':k','LineWidth',0.2);
% make a legend for the 1st line returned from
% each plot call (i.e., first row of h)
legend(h(1,:),{'540' '720' '900' '1080'})

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

カテゴリ

製品

リリース

R2016a

タグ

質問済み:

2022 年 5 月 31 日

編集済み:

2022 年 5 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by