Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 10

Task 10.2 Detail: Adding calendar labels to plots

Summary of new tools and commands.

Task: Determine the calendar date for base date for the MATLAB time base (MATLAB day zero). What is the julianday for that calendar date? What is the calendar date for Julian Day zero? How many days has it been between today and Julian Day zero? What is the Julian date and modified Julian date of your birthday?

Julian dates (abbreviated JD) are simply a continuous count of days and fractions since noon Universal Time on January 1, 4713 BCE (on the Gregorian calendar). Almost 2.5 million days have passed since this date. Julian dates are widely used as the time variable within the astronomical community which deals with extended spans of time.

A Julian date can be obtained in any of the following ways, which requires the creation of a date string with datetime(year,month,day) or datetime(year,month,day,hour,minute,second).

D=datetime(yr,mo,day);jd=juliandate(D);
D=datetime(yr,mo,day,hr,min,sec);jd=juliandate(D);
jd=juliandate(datetime(yr,mo,day,hr,min,sec));

A modified version of the Julian date denoted MJD is obtained by subtracting 2,400,000.5 days from the Julian date JD, The MJD therefore gives the number of days since midnight on November 17, 1858. This date corresponds to 2400000.5 days after day 0 of the Julian calendar. These modified JD are simply shorter numbers for more recent times.

A modified Julian date can be obtained in any of the following ways:

D=datetime(yr,mo,day);mjd=mjuliandate(D);
D=datetime(yr,mo,day,hr,min,sec);mjd=mjuliandate(D);
mjd=mjuliandate(datetime(yr,mo,day,hr,min,sec));
mjuliandate() is provided in the Aerospace toolbox.

There are times when we need to put calendar dates on plots, which can be rather painful as you can imagine. MATLAB has a nice tool called datetick(tickaxis,dateform) to accomplish this. The first argument, tickaxis, should be one of three characters: 'x', 'y', 'z', which indicates which axis on the plot gets the calendar information.

The second argument gives the desired format of the calendar date. It can be an integer (0 to 31) or a text string. Look at help datetick to see all of the options for this command.

The text string is composed of letters y,m,d,H,M,S indicating year, month, day, hour, minute, second. If the text string is 'dd-mmm-yyyy' then the ticks will be labeled with the day, the month as a 3 letter abbreviation and the year as 4 numbers, with dashes between. If there is only one m in the string, then the first letter of the name of the month is used.

If the string has only 2 y's (yy) then only the last two digits of the year are used in the label.

There are too many options to go through all of them, but you should get the idea that you can construct almost any style of calendar date on the axis.

One other warning, the longer the string showing the date, the fewer tickmarks there will be.

As an example, consider the following commands:

t1=datenum(2010,5,1);
t2=datenum(2011,3,31);
time=t1:t2;
x=1+cos(time/15);
plot(time,x);
datetick('x','mmmdd');  %  month abbrev with day numbers
title('Example for datetick')

   SS10_2a

datetick() often changes the number of tick marks or the range of values on the axis. To preserve the number of tick marks, add the option 'keepticks'. To preserve the axis scale, use 'keeplimits'.

A possible command would be datetick('x','mmmdd','keepticks','keeplimits');

Flow chart for the task:

%%%  get calendar date for mtime=0
%%%  get datetime for mtime=0
%%%  get julian date for that time
%%%  get julian date for today
%%%  get julian date for birthday
%%%  get modified julian date for birthday

Solution script for task:

%%%  get calendar date for mtime=0
disp(['Matlab day 0 is ' datestr(0)])
%%%  get datetime for mtime=0
T=datetime(datevec(0));
%%%  get julian date for that time
disp(['Julian date for Matlab day 0 ' num2str(juliandate(T))])
%%%  get julian date for today
disp(['Julian date for today is ' num2str(juliandate(datetime(2021,11,1)))])
%%%  get julian date for birthday
disp(['Julian date for my birthday is ' num2str(juliandate(datetime(1960,1,2)))])
%%%  get modified julian date for birthday
disp(['Mod Julian date for my birthday is ' num2str(mjuliandate(datetime(1960,1,2)))])

Matlab Class Home      Class Outline      Previous Task      Next Task      Main Class Page      Evaluation 10


email: J. Klinck