Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Excel date column export into Matlab

  1. Sep 17, 2013 #1
    Hi everybody,
    I need assistance. I have excel file that conntains a date column and another column. I wish to plot the date on x axis and the second column on y axis. I wrote the matlab code below.

    This is my code. data=xlsread('DataTest','Sheet1'); q=data(:,2); time=data(:,1);

    Error message

    Attempted to access data(:,2); index out of bounds because size(data)=[193,1].

    Matlab is not reading the first column of the excel file that contains the date so it takes the second column as the first column as a result it could not find second column. Please help
  2. jcsd
  3. Sep 17, 2013 #2


    User Avatar
    Gold Member

  4. Sep 17, 2013 #3


    User Avatar
    2017 Award

    Staff: Mentor

    As a workaround, what happens if you add an empty column in the spreadsheet?
  5. Sep 18, 2013 #4
    Thanks Kreil,
    I have succeeded in using datenum. See the code below
    datecol = 1; wt(:,datecol) = wt(:,datecol) + datenum('30-Dec-1899'); x=wt(:,1); qo=wt(:,2); plot(x,qo);

    How do I use datetick to get the x (date axis) in day-month-year ('dd-mmm-yyyy') format?
    Thanks Isa
  6. Sep 19, 2013 #5


    User Avatar
    Gold Member

    datetick is a function for adding the date to tick labels on a plot.

    Use the datestr() function to reverse what datenum() does. For example,

    Code (Text):

    >> d = datenum('30-Dec-1899')

    d =


    >> datestr(d)

    ans =

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook