customised x data in matlab plot

I have an x label ( starting from 0 in the axis - 1000, 2000, 3000, 4000, 50000, 6000)
I want to change the axis to the following starting from 0 in the axis - 2000, 4000, 6000, 8000, 10000, 12000
I have used the following implementation but not working
labels = [0 2000 4000 6000 8000 10000 12000];
disp(plot(X));
set(gca, 'XTick', 1:length(labels));
set(gca, 'XTickLabel', labels);
Can someone help me out with this
Thanks in advance

 採用された回答

Rik
Rik 2019 年 5 月 28 日

0 投票

The XTick property requires a vector with numeric data, but the XTickLabel property requires a cell array.
labels = [0 2000 4000 6000 8000 10000 12000];
disp(plot(X));
set(gca, 'XTick', 1:numel(labels));%this puts the labels at x=1,2,3,4,5,6,7
set(gca, 'XTickLabel', cellfun(@num2str,num2cell(labels));

8 件のコメント

Tino
Tino 2019 年 5 月 28 日
Hi Rik
Thanks for your swift response. I am getting the error
Error using cellfun
Non-scalar in Uniform output, at index 2, output 1.
set 'UniformOutput' to false.
Error in nuss(line 35)
set(gca, 'XTickLabel', cellfun(@num2str,num2cell(labels)));
Thanks in advance
Tino
Tino
Tino 2019 年 5 月 28 日
Hi Rik
I was able to put the Uniform output false.
But when the plot the graph the numbers all clustered to the axis of the x axis. what is the issue ?
Hope to hear from you soon
Regards
Tino
Rik
Rik 2019 年 5 月 28 日
Sorry for the typo (missing the UniformOutput).
If you only set the XTick property, you don't actually need to set the label. It looked like you wanted to have the label '2000' show up at x=2, so my code does that for you.
So maybe this code is what you meant?
labels = [0 2000 4000 6000 8000 10000 12000];
disp(plot(X));
set(gca, 'XTick', labels);
%this is now optional:
set(gca, 'XTickLabel', cellfun(@num2str,num2cell(labels),'UniformOutput',0);
Tino
Tino 2019 年 5 月 28 日
Thanks for your response Rik.
Am sorry maybe you did not understand my question
I want to 0 1000, 2000, 3000, 4000, 50000, 6000
0 2000, 4000, 6000, 8000, 10,000, 12,000
The numbers down to replace the number up in the graph
I hope this is clearer. where I have 1000, I want to make it 2000 and so on
Regards
Tino
Rik
Rik 2019 年 5 月 28 日
So where the actual x-postition is 1000 you want to have the label say 2000 etc?
Tino
Tino 2019 年 5 月 28 日
Hi Rik
That is exactly what I mean
where it is 1000 i want it to be 2000
and where it is 2000 I want it to be 4000 etc
Thanks for your help in advance
Regards
tino
Rik
Rik 2019 年 5 月 28 日
Then you can use this:
labels_real = [0 1000 2000 3000 4000 5000 6000];
labels_fake = [0 2000 4000 6000 8000 10000 12000];
%or just:
%labels_real=0:1000:6000;
%labels_fake=0:2000:12000;
%or even:
%labels_real=0:1000:6000;
%labels_fake=labels_real/2;
disp(plot(X));
set(gca, 'XTick', labels_real);
%this is now optional:
set(gca, 'XTickLabel', cellfun(@num2str,num2cell(labels_fake),'UniformOutput',0);
Tino
Tino 2019 年 5 月 28 日
Thank you Rik

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

その他の回答 (0 件)

カテゴリ

製品

リリース

R2019a

タグ

質問済み:

2019 年 5 月 28 日

コメント済み:

2019 年 5 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by