Plotting time vector in seconds on x-axis in HMS

3 views (last 30 days)
Hello,
I have a time vector that I use together with my data. I have RMS data every 4 seconds so the time vector only has values every 4 seconds. As I plot my data in function of the time vector, I want to get Hour:Minutes:seconds on the x-axis instead of only seconds. I tried functions but none of them seems to give good results, can anyone help me?
tRms=0,4,8,12,16,20,24,........... (1x300 double)
vRunRmsCf = (22x300 double)
semilogy(tRms,vRunRmsCf(selectPlotCf,:))
xlim([0 max(tRms)]);
ylim([0.0000001 0.0001]);
xlabel( 'Time [s] ' );
ylabel( 'RMS Velocity [m/s] ' );

Answers (1)

dpb
dpb on 23 Feb 2016
Prior to R2015, use datenum and convert to datenumbers and plot against them and then datetick to display the axis with time markings.
Newer versions have the datetime class with builtin methods including plot being time-aware...
  1 Comment
Peter Perkins
Peter Perkins on 24 Feb 2016
In R2014b or later, duration might be a better choice than datetime, since the times are really elapsed time since 0. Unfortunately, the semilogx function doesn't currently accept datetimes or durations, only plot does.
However, you might still be able to leverage duration just to get the tick labels you want:
tRms = seconds(0:4:299*4);
vRunRmsCf = rand(22,300);
semilogy(seconds(tRms),vRunRmsCf)
xlim(seconds([0 max(tRms)]));
ylim([0.0000001 0.0001]);
xlabel( 'Time [hh:mm:ss] ' );
ylabel( 'RMS Velocity [m/s] ' );
xticks = get(gca,'XTick');
xticklabels = cellstr(duration(0,0,xticks));
set(gca,'XTickLabel',xticklabels)

Sign in to comment.

Categories

Find more on Dates and Time in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!