MATLAB Answers

How do I control the xticks if x is a long imported excel column (Time Format: HH:MM:SS)?

4 views (last 30 days)
Ramo Rafsel
Ramo Rafsel on 12 Aug 2020
Answered: Shae Morgan on 18 Aug 2020
How can I show out of the 1700 values of a time column (in HH:MM:SS format), only a couple of them in the x axis? Do I have to change the time format into a double value to define how it should be in the x axis?

  3 Comments

Shae Morgan
Shae Morgan on 13 Aug 2020
Try this out: Since I didn't have your variable class, I had to assume you're dealing with time-stamps here. This works for those:
t1 = datetime(2020,8,12,13,11,24); %start time
t2 = datetime(2020,8,12,13,18,36); %end time
time5 = t1:seconds:t2; %create a vector of time points from the start time to the end time
v5 = randn(size(time5)); %create random y-axis data
plot(time5,v5) %plot data
hold on; %hold the current figure so the first plot doesn't erase when you do the 2nd plot
avg5 = mean(v5); %the average value of v5
x2= [time5(1) time5(end)]; %x-values for the average line
plot(x2,[avg5 avg5], 'y-', 'LineWidth',2); %plot the avg line
title('value 5 by time plot') %title the plot
legend('Raw data','Average line') %add a legend to differentiate the two data sources
ax = gca; %get current axis
x_step_size=100; %step size between labels in seconds
ax.XAxis.TickValues = t1:seconds(x_step_size):t2; %adjust the tick values to the correct spacing

Sign in to comment.

Accepted Answer

Shae Morgan
Shae Morgan on 18 Aug 2020
Try this out: Since I didn't have your variable class, I had to assume you're dealing with time-stamps here. This works for those:
t1 = datetime(2020,8,12,13,11,24); %start time
t2 = datetime(2020,8,12,13,18,36); %end time
time5 = t1:seconds:t2; %create a vector of time points from the start time to the end time
v5 = randn(size(time5)); %create random y-axis data
plot(time5,v5) %plot data
hold on; %hold the current figure so the first plot doesn't erase when you do the 2nd plot
avg5 = mean(v5); %the average value of v5
x2= [time5(1) time5(end)]; %x-values for the average line
plot(x2,[avg5 avg5], 'y-', 'LineWidth',2); %plot the avg line
title('value 5 by time plot') %title the plot
legend('Raw data','Average line') %add a legend to differentiate the two data sources
ax = gca; %get current axis
x_step_size=100; %step size between labels in seconds
ax.XAxis.TickValues = t1:seconds(x_step_size):t2; %adjust the tick values to the correct spacing

  0 Comments

Sign in to comment.

More Answers (1)

Madhav Thakker
Madhav Thakker on 18 Aug 2020
Hi
You can use xtickformat which can match the excel sheet format. You can use xticksto specify different values on the x-axis. I am attaching a MATLAB code snippet that does the same.
t = 0:seconds(30):minutes(3);
y = rand(1,7);
plot(t,y)
xticks([t(1) t(4) t(6)])
xtickformat('hh:mm:ss')
Hope this helps.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!