nori
nori

Reputation: 59

Matlab - datetick, datenum issues

I am trying to plot a bunch of data that are 2 min averages and I want to have the label on the xaxis show day/month hour:mins. I can get some dates to show up on the xaxis but there are a few problems:

  1. location of xticks does not coincide with my data (see code snippet)
  2. in my real data i have +1000 points so i would like to label in certain time intervals, say every 12hrs.

below is a sample. any help would be greatly welcome.

%make date vec
ds = {'28/01/2000 11:52:00';'28/01/2000 11:54:00';'28/01/2000 11:56:00'};
x = datenum(ds,'dd/mm/yyyy HH:MM:SS')

y = [1,2,3]; %data
plot (x,y,'x')

%define start and end date for xAxis
startDate = x(1)
endDate = x(end)
xdata = linspace(startDate,endDate,5)
set(gca,'XTick',xdata)
datetick('x','dd/yy HH:MM')

Upvotes: 2

Views: 5696

Answers (3)

Lotfi
Lotfi

Reputation: 1

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');

Upvotes: 0

Gunther Struyf
Gunther Struyf

Reputation: 11168

Add the option keepticks to the datetick call. Fixed things up here on my R2012a:

datetick('x','dd/yy HH:MM','keepticks')

Source: this and datetick doc

Upvotes: 1

Dan
Dan

Reputation: 45752

One solution is instead of using datetick() set the ticklabels your self. So replace your last line of code with these two lines:

    dsdata = datestr(xdata, 'dd/yy HH:MM');
    set(gca,'XTickLabel',dsdata);

Upvotes: 0

Related Questions