How do I change the x-axis tick marks and grid lines to occur every minute for a plot?

57 views (last 30 days)
I'm working on a script that plots data against time. The data was read in from a text file, and the times were determined by first changing a string of times to a character array, and then to numbers. Finally those numbers were converted to times in unix time, which is in seconds since 1970. I would like the x-axis to contain ticks and grid lines at each minute, with labels in the form of HH:MM:SS (not necessarily at each minute, but at least 3 points during the plot). Here is the code I have so far:
%Plot
figure(1)
timeArray=gyroDriftArray{1};
UTtimeCharArray = char(timeArray);
years = str2num(UTtimeCharArray(:,1:4));
months = str2num(UTtimeCharArray(:,6:7));
days = str2num(UTtimeCharArray(:,9:10));
hours = str2num(UTtimeCharArray(:,12:13));
minutes = str2num(UTtimeCharArray(:,15:16));
seconds = str2num(UTtimeCharArray(:,18:21));
times = datenum(years,months,days,hours,minutes,seconds);
subplot(3,1,1)
plot1=plot(times,ta_gyro_drift_el,'b', times,ta_gyro_drift_xel,'g',times,ta_gyro_drift_los, 'r');
grid on
legend(plot1, 'ta\_ gyro\_ drift\_el','ta\_gyro\_drift\_xel * -1','times\_ta\_gyro\_drift\_los','Location','Best');
datetick('x','HH:MM:SS', 'keeplimits')
title(['TA Gyroscopic Drift' UTtimeCharArray(1,12:19) ' --> ' UTtimeCharArray(end,12:19) ' UT '],'FontSize',10)
xlim([min(times), max(times)])
xlabel('Time (UT)');
ylabel('Drift (arcsec)');
hold on
I have included the datetick command in here, which is setting the ticks and grid lines. I have also tried to use the following in place of datetick:
set(gca,'XTick',min(times):max(times))
However, I'm unsure of how to maintain the HH:MM:SS format for the ticks, while having it increase in minutes. Ideally, I would like the labels to read 07:01:00 07:02:00 07:03:00 ... 07:40:00 (but not necessarily include all labels, just make sure that each gridline and tick occurs at each minute). Thank you for the help in advance.

Answers (2)

Lotfi
Lotfi on 14 Dec 2013
Here is what worked for me to get ticks every minute
step=2.5/3600; % minute
debut = (floor(pts_dthgps(1)/step))*step;
fin = (floor(pts_dthgps(sz)/step))*step;
figure(2);
h=plot(pts_dthgps, pts_vitgps);
set(gca, 'XTick', [debut:step:fin]);
datetick('x','HH:MM','keeplimits','keepticks');

dpb
dpb on 17 Jul 2013
Edited: dpb on 17 Jul 2013
Set the 'xtick' property to the datenum values determined from the date/time from xmin:xmax desired. Easiest is to just use the start y,m,d,h,min,sec excepting for the min instead of the constant start use a vector of number of minutes wanted...if it's 2 hours that would be 121 values [0:120]' if the origin is at 00 minutes--if not use the desired offset added to the 0:N vector. This works because datenum() is smart enough to automagically wrap fields that overflow the magnitude of the range for the field and to percolate the value to the next higher field accounting for all the nuances including leap year.
Using this resulting array will set the tick marks.
Then, as that will probably be too busy to be able to read the full string, set the 'xticklabel' property to the datestr array returned from using the xtick array at every nth point -- since, unfortunately, can't set an individual tick label indexing the array, need to create a vector of blanks of the same length as the xtick vector then fill in the ones desired. SOTOO
tlab=cellstr(repmat(' ',length(t),1)); % blank cell array
tlab(1:10:end)=cellstr(datestr(t(1:10:end),'HH:MM:SS'));
set(gca,'xticklabel',tlab)
Oh, just thought ... I'd have to look up exactly how minor ticks work; on a plot test here just setting the xtick to t(1:10:end) and then dlab for those (w/o the trouble of the full-length blank array) and then
set(gca,'xminortick','on')
set the number of ticks between the two at 1-min intervals. As axis limits change I'd guess this might only give 2-min or other intervals.
OK, here's a toy example to try...
dn=datenum(2000,5,1,3,12+[0:40]',0); % time axis datenums
xtix=dn(1:10:end) % postions for date axis ticks
xlab=cellstr(datestr(xtix),'HH:MM:SS')); % x-axis date labels
plot(dn,rand(size(dn))) % a plot
xlim([dn(1) dn(end)]) % set limits to timeseries range
datetick('x','HH:MM:SS','keeplimits') % and axis to time, keeping above
set(gca,'xtick',xtix) % switch to time labels
set(gca,'xticklabel',xlab) % write the time strings
set(gca,'xminortick','on') % put on the minor ticks
As always, "salt to suit"... :)
  1 Comment
Sarah
Sarah on 26 Jul 2013
I apologize for not "accepting" this answer sooner. I will be continuing on this section of my script next week and will make comments then.

Sign in to comment.

Categories

Find more on Graphics 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!