Search code examples
matlabdatetimetime-seriesimport-from-excel

Importing Excel Data with Dates and Values and Plotting Time Series in Matlab


I would like to plot a time series in Matlab of a data set I have in Excel.

The Excel file looks as follows:

Data:       |  Value:

2005-04-01  |  5.20

2006-12-02  |  3.12

...

How could I load this into Matlab and plot the time series of it?


Solution

  • There's 2 easy way of plotting dates, but I'll give you the script to read from the xls file first.

    % Read from Excel
    [N,T] = xlsread( filepath );
    

    You then need to extract/convert the dates as follows. Dates are the 1st column of the text.

    d = datetime( T(:,1) );
    

    Then you can plot the variables as follows

    figure;
    plot( d, N(:,1) );
    

    A sample plot is here

    Date Plot

    Alternatively, you can use datenum instead of datetime if you want the date as an integer instead of a datetime object using the following line.

    d = datenum( T(:,1) );