0

So one thing that bothers me is about MATLAB figures is that timestamps are a challenge to render properly.

For instance

time = now:1/24/3600:now+1;
xval  = sin((time-time(1)).^2*2*pi) + cos(2*(time-time(1)).^4*2*pi);
plot(time, xval)
set(gca, 'XTickLabel', datestr(get(gca,'Xtick'),'HH:MM'));

enter image description here

Now the problem with this is that xlabels will not adjust properly when I pan or zoom:

enter image description here

Rather what you see is the same tick labels being recycled... This of course come back to the fact that the program does what you tell it to do....

What I have to do is call set(gca, 'XTickLabel', datestr(get(gca,'Xtick'),'HH:MM')); again...

But I'm lazy bum and I don't want to do this everytime I pan and zoom. I want it to be programmed in the figure, somehow.

Now figures has this wonderful thing called WindowButtonMotionFcn, from which I can call another function:

set(gcf,'WindowButtonMotionFcn', @plot_time_change)

function plot_time_change (gcbo, eventdata, handles)  
set(gca,'XTickLabel',datestr(get(gca,'Xtick'),'HH:MM'));
end

This works OK, but it is not visually elegant, especially given that it doesn't get called when I zoom.

So the question is: are there better solutions that I'm missing out on?

Rasman
  • 5,349
  • 1
  • 25
  • 38

1 Answers1

0

Set the post call back action for zoom to your plot_time_change function

set(zoom(gca),'ActionPostCallback',@plot_time_change)

see this previous question for more

Community
  • 1
  • 1
RTL
  • 3,577
  • 15
  • 24