I feel your pain. I used to deal with date on x-axis and it is really a huge, huge pain.
I suggest you go to File Exchange and grab the function called: xticklabelrotate
That function can actually rotate your x-axis at any angle you want. For dates, I go for a 90 degrees rotation.
Using datetick straight away will create a lot of problems. For me, I am tired of doing that so I experimented my own way. Somehow I achieved great feats with the following procedure:-
1) Convert your time matrix into strings using datestr BEFORE you plot it. Or below I provide my way of doing it which is doing it on the spot. Change the codes the way you want;
2) Determine how many x-ticks you want. If you want 21 x-ticks, then generate a so-called spaceData as follows:-
spaceData = linspace(1,x_lim,20);
spaceData = round(spaceData);
Here my x_lim is the last of my Time matrix. It is acquired by:-
3)Then create the xData:-
xData(i) = cellstr(datestr(time(spaceData(i))));
What I am doing here is changing the time value at the locations notified by spaceData into strings. Then 'trap' them into cellstring.
4)And then I generate the following two matrices:
xdata_gen = zeros(x_lim,1);
xdata_gen(i) = i;
Mind that x_lim here is not 21 but the length of the time matrix. What I am doing here is generating the exact number of xdata_gen elements as that of the variable time.
xdata_gen is just a number matrix from 1,2,3,4,5......x_lim
5. Here comes the magic:-
hl1 = bar(xdata_gen,y);
ax1 = gca;
So the trick is to plot the numbered matrix against y (or your flowrate, then tell Matlab that you only want 21 ticks and their positions on x-axis.
After that change all the numbers into the cellstr date matrix.
After that rotate the x-axis labels.
Sounds a little complicated but it really works.