Question: I have a two column data with `mmyyyy` and `SPI` (Standardized Precipitation Index) variables. The first two samples have no data (`NAN`). The file is:

```
011982 NAN
021982 NAN
031982 -1.348
.
.
.
122013 1.098

```

I load the time and SPI data into MATLAB, then I would like to plot it but it is not working.

I would like to plot line graph but I really have no idea how to plot time in x-axis and I would like my x-axis to show only the year.

Answer: 
Using the new [`datetime`](http://www.mathworks.com/help/matlab/date-and-time-operations.html) data type in MATLAB (added in R2014b), this should be easy.

Here is an example. First we load the data into a MATLAB [`table`](http://www.mathworks.com/help/matlab/tables.html):

```
% import data from file
fid = fopen('file.dat', 'rt');
C = textscan(fid, '%{MMyyyy}D %f');
fclose(fid);

% create table
t = table(C{:}, 'VariableNames',{'date','SPI'});

```

You get something like this:

```
>> t(1:10,:)
ans = 
     date       SPI   
    ______    ________
    011982         NaN
    021982         NaN
    031982       2.022
    041982      1.5689
    051982     0.75813
    061982    -0.74338
    071982     -1.7323
    081982     -2.4466
    091982    -0.86604
    101982    0.085698

```

Next to [plot the data with date and time](http://www.mathworks.com/help/matlab/matlab_prog/plot-dates-and-durations.html), it's as easy as calling `plot`:

```
plot(t.date, t.SPI)
xlabel('Date'), ylabel('Standardized Precipitation Index')

```

![plot](https://i.stack.imgur.com/QqV7s.png)

By default, `plot` chooses tick mark locations based on the range of data. When you zoom in and out of a plot, the tick labels automatically adjust to the new axis limits.

But if you want, you can also specify a [custom format](http://www.mathworks.com/help/matlab/ref/plot.html#input_argument_namevalue_datetimetickformat) for the `datetime` tick labels. Note that when you do this, the plot always formats the tick labels according to the specified value, they won't adjust on zoom:

```
plot(t.date, t.SPI, 'DatetimeTickFormat','yyyy')

```
I'm adding another answer that works in older MATLAB versions without `table` or `datetime` data types.

Like before, we first import the data from file, but this time we read the dates as strings then convert them to *serial date numbers* using [`datenum`](http://www.mathworks.com/help/matlab/ref/datenum.html) function (defined as the number of days since "January 0, 0000"):

```
% import data from file
fid = fopen('file.dat', 'rt');
C = textscan(fid, '%s %f');
fclose(fid);

% create matrix
t = [datenum(C{1},'mmyyyy') C{2}];

```

The data looks like this:

```
>> format long 
>> t(1:10,:)
ans =
   1.0e+05 *
   7.239120000000000                 NaN
   7.239430000000000                 NaN
   7.239710000000000   0.000005606888474
   7.240020000000000   0.000009156147863
   7.240320000000000   0.000004504804864
   7.240630000000000   0.000008359005819
   7.240930000000000   0.000007436313932
   7.241240000000000   0.000002800134237
   7.241550000000000   0.000005261613664
   7.241850000000000   0.000001809901372

```

Next we plot the data like before, but instead we use the [`datetick`](http://www.mathworks.com/help/matlab/ref/datetick.html) function to format the x-axis as dates ('yyyy' for years):

```
plot(t(:,1), t(:,2))
datetick('x', 'yyyy')
xlabel('Date'), ylabel('Standardized Precipitation Index')

```

![plot](https://i.stack.imgur.com/MAhrh.png)

Unfortunately the tick labels will not automatically update when you zoom in and out... The good news, there are solutions on the File Exchange that solve this issue, for example [datetickzoom](http://www.mathworks.com/matlabcentral/fileexchange/15029-datetickzoom-automatically-update-dateticks) and [datetick2](http://www.mathworks.com/matlabcentral/fileexchange/20422-datetick-with-zooming-panning-and-subplots).