How i can change the x-axis labels and ticks like that

4 views (last 30 days)
Hello , i have been trying to change my code to get the xticks and xlabels in this image but i dont know what to do
Here is the picture
and here is my picture with the code below it
clc
clear all
close all
dataset = xlsread('Final2.xlsx','Sheet1','C1:D121')
hour = dataset(:,1)
kp = [dataset(:,2)]
t=linspace(0,24,120)
subplot(5,1,1)
bar(hour,kp)
ylabel 'kp index'
grid on
xticks ([0:4:120])
xticklabels({'4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24'})
data = xlsread('Final2.xlsx','Sheet2','D2:I7201')
mins = data(:,1)
Bz = data(:,2)
subplot(5,1,2)
plot(mins,Bz)
%xticks ([s])
%xticklabels({'4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24'})
ylabel 'Bz(nT)'
Vx= data(:,3)
subplot(5,1,3)
plot(mins,Vx)
ylabel 'Vx Km\s'
Al = data(:,4)
Au = data(:,5)
subplot(5,1,4)
hold on
plot(mins,Au)
plot(mins,Al)
ylabel 'AU & AL(nT)'
SYM = data(:,6)
subplot(5,1,5)
plot(mins,SYM)
ylabel 'SYM-H(nT)'
xlabel 'UT(hr)'
%xticks(0:4:120)
%xticklabels({'4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24'})
how i can change the xlabels and xticks to be '0','4','8','12','16','20','24' and repeat itself

Accepted Answer

Adam Danz
Adam Danz on 13 Nov 2019
If you're using Matlab r2019b or later, you can use tiledlayout() to create your subplots (see examples in that link).
For earlier releases, you can use linkaxes() to set equal axis limits between all subplots. You'll need the axis handles which would look something like this
sbp(1) = subplot(5,1,1);
% Your code
sbp(2) = subplot(5,1,2);
% Your code
sbp(3) = subplot(5,1,3);
% Your code etc.....
% Then, after all subplots are created,
linkaxes(sbp, 'x')
  4 Comments
Adam Danz
Adam Danz on 13 Nov 2019
Edited: Adam Danz on 15 Nov 2019
Those are 3 different options or ideas, not 3 steps.
I actually don't recommend option 3. Options 1 and 2 are much cleaner and easier.
Here's an example of option 3. The x axis values are actually 1,2,3,4,5 but I've altering their labels to appear as 10,20,30,40,50. If you add a new value at x=40, you won't see it because it will be way off the figure.
figure()
x = [1 2 3 4 5];
y = [1 3 14 29 48];
plot(x,y, 'o')
set(gca, 'XTick', 1:5, 'XTickLabel', {'10' '20' '30' '40' '50'})

Sign in to comment.

More Answers (1)

samer hisham
samer hisham on 13 Nov 2019
Well i dont think it worked for me , or i typed it wrong nope.png
clc
clear all
close all
dataset = xlsread('Final2.xlsx','Sheet1','C1:D121')
hour = dataset(:,1)
kp = [dataset(:,2)]
t=linspace(0,24,120)
sbp(1)= subplot(5,1,1)
bar(hour,kp)
ylabel 'kp index'
grid on
xticks ([0:4:120])
xticklabels({'4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24'})
data = xlsread('Final2.xlsx','Sheet2','D2:I7201')
mins = data(:,1)
Bz = data(:,2)
sbp(2)=subplot(5,1,2)
plot(mins,Bz)
%xticks ([s])
%xticklabels({'4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24'})
ylabel 'Bz(nT)'
Vx= data(:,3)
sbp(3)=subplot(5,1,3)
plot(mins,Vx)
ylabel 'Vx Km\s'
Al = data(:,4)
Au = data(:,5)
sbp(4)=subplot(5,1,4)
hold on
plot(mins,Au)
plot(mins,Al)
ylabel 'AU & AL(nT)'
SYM = data(:,6)
sbp(5)=subplot(5,1,5)
plot(mins,SYM)
ylabel 'SYM-H(nT)'
xlabel 'UT(hr)'
%xticks(0:4:120)
%xticklabels({'4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24','4','8','12','16','20','24'})
linkaxes(sbp, 'x')
  1 Comment
Adam Danz
Adam Danz on 13 Nov 2019
I replied under my answer. Please use the comment sections for comments and discussion and reserve the answer sections for answers.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!