No BSD License  

Highlights from
PlotDateTime

image thumbnail
from PlotDateTime by Simon Bridger
Puts actual dates and times onto a plot.

PlotDateTime(StartDateNum,EndDateNum);
function PlotDateTime(StartDateNum,EndDateNum);
% PlotDateTime([StartDateNum,EndDateNum]) Plot times/dates on time series graphs in vertical text
% Plots time every hour, and date at midnight and first position...
% Only puts about 24 marks max on each plot. Marks always fall on midnight
% draws on the current axis....
% PlotDateTime() assumes that xaxis is a vaild datenum
% PlotDateTime(StartDateNum) assumes xaxis is in days from 0
% PlotDateNum(Start,End) assumes scale is not in days 
% PlotDateNum('SelfTest') runs the demo
%
% sets an appropriate default xlabel if it can work it out from:
% time, date, days, hours, mins ,secs
% this means that "xlabel" should be placed after plotdatetime
% for very short periods forces times with seconds onto plot

%see also: Genplot, dateaxis datestr datenum now

%SJB $Revision: 1.2 $ $Date: 2002-02-28 01:57:04+13 $

TextRotateAngle=90;
hax=gca;
xlim=get(hax,'xlim');
RawTimeAxis=0;
switch nargin 
  case 0; % must be plotted in datenum units (ie days)
         StartDateNum=xlim(1);
         EndDateNum=xlim(2);
         StartX=xlim(1); XScale=1;
         RawTimeAxis=1;
  case 1,  
         if isstr(StartDateNum)
           switch lower(StartDateNum)
             case {'selftest','demo'}, plotdatetime_SelfTest; return;
             otherwise error(['unknown parameter :',StartDateNum]);
           end; %switch
         end;%if
         % xaxis must be in Days from StartDateNum
         EndDateNum=xlim(2)+StartDateNum;
         StartDateNum=xlim(1)+StartDateNum;
         StartX=xlim(1); XScale=1;
  case 2, StartX=xlim(1); 
          XScale= (EndDateNum-StartDateNum) / (xlim(2)-xlim(1)); 
  otherwise
    error('wrong number of arguments');
end; %switch
% Change text formats here.....
DateFormat=1;
TimeFormat=16;
ax=axis;  %get current axis extent
YPos=ax(4) - (ax(4)-ax(3))/3;

%StartDateNum=Times(1)
%EndDateNum=Times(length(Times))

% number of 1 hour marks to plot...
NHrs=24*( (floor(EndDateNum*24)/24)-(ceil(StartDateNum*24)/24) );
HrsPerMark=ceil(1/24*NHrs);
% now ensure that only about 24 marks per graph max, and that marks will fall on Hr00
switch HrsPerMark
  case 0, HrsPerMark=1;
  case 5
    HrsPerMark=6;
  case 7
    HrsPerMark=8;
  case {9,10,11,13,14}
    HrsPerMark=12;
  otherwise
    if HrsPerMark>=15
      HrsPerMark=ceil(HrsPerMark/24)*24;
    end;
end; %switch
HrsPerMark;
MPD=24/HrsPerMark;  %MarksPerDay
TimeFormat=16;      %16 'HH:MM PM' 3:45 PM

% At last write them on the graph
% every hour print the time, every day print the date....
first=1;
TickDateNums=(ceil(StartDateNum*MPD)/MPD) :(1/MPD) : (floor(EndDateNum*MPD)/MPD);
if isempty(TickDateNums) %very short periods
    TimeSpan=EndDateNum-StartDateNum;
    if TimeSpan<1/24
        TimeFormat=13; %13  'HH:MM:SS' 15:45:17
    end;%if
    TickDateNums=StartDateNum+ (TimeSpan*[0.05,.1:.2:.9]);
end;%if
%datestr(TickDateNums)

for DN= TickDateNums
  if (mod(DN,1)< 0.5/MPD) | first %if hour=0  or first one...
      S=DateStr(DN,1);         %show date
    else
      S=DateStr(DN,TimeFormat);        %else show time
    end;
  t1=text((DN-StartDateNum)/XScale +xlim(1),YPos,S);
  set(t1,'rotation',TextRotateAngle,'fontsize',6,'fontweight','bold');
  first=0; %clear flag
end; %for
if RawTimeAxis %x axis is in matlab time units
    set(hax,'xtickmode','manual','xtick',TickDateNums ,'xticklabel',{});
    if NHrs<24
        xlabel('time');
      else
        xlabel('date');
    end;%if
  else
  %XScale=XScale
  %round(XScale*24*3600)
    switch round(XScale*24*3600)
      case 24*3600, xlabel('days');
      case 3600, xlabel('hours')
      case 60, xlabel('mins')
      case 1, xlabel('secs')
      otherwise
    end; %switch
end;%if
%dateaxis('x',0,0);


%<begin SelfTest>--------------------------------------------------
function plotdatetime_SelfTest
%
%dbstop if error; %so you can inspect vars when it crashes
fprintf(1,'\n--------------Testing plotdatetime: Tests that should work  --------------------\n');
HR=1/(24);
MIN=1/(24*60);
SEC=1/(24*3600);
%figure
subplot(2,2,1);
q=[0:0.15:3];
plot(now+q,q,'r');
axis tight;
plotdatetime;
subplot(2,2,2);
plot(q,-q,'g');
axis tight;
plotdatetime(now);

%now demo the auto setting of the xlabel, and short span display
figure
q=[-0.1:1:100];
subplot(2,2,1);
plot(q,q,'r');
axis tight;
plotdatetime(now,now+(100));
subplot(2,2,2);
plot(q,q,'g');
axis tight;
plotdatetime(now,now+(100*HR));
subplot(2,2,3);
plot(q,q,'b');
axis tight;
plotdatetime(now,now+(100*MIN));

subplot(2,2,4);
plot(q,-q,'m');
axis tight;
plotdatetime(now,now+(100*SEC));

%if
%  warning('failed trying to  at(1)');
%  keyboard;
%end;%if
%fprintf(1,'\n-------------- Tests that SHOULD throw errors  --------------------\n');
%try

%  warning('failed with invalid state number 4');
%  keyboard;
%catch
%  fprintf(1,[lasterr,'\n^Should have errored: \n']);
%end; %try

fprintf(1,['-------- Seems to have worked OK',' -----------\n']);

Contact us at files@mathworks.com