How to read a spreadsheet with date and time in Matlab?

2

How do I import data from an excel sheet containing date and time (eg 10/22/14 22:45:10) in Matlab?

    
asked by anonymous 09.10.2014 / 04:08

1 answer

2

I recommend:

  • use the xlsread command,
  • or convert the file to csv format and then use the csvread command,
  • or if none of these solutions satisfies you you can use the readtable.
  • For the latter, I'll show you how to import an excel file containing dates into a table in Matlab. Be the hypothetical peso.xls file with the following data.

       Data        Peso
    ___________    ______
    
    31-Oct-1996    174.8 
    29-Nov-1996    179.3 
    30-Dec-1996    190.4 
    31-Jan-1997    185.7 
    

    Now do the following.

    T = readtable ('peso.xls')

    T =

        Data        Peso
    ____________    ______
    
    '10/31/1996'    174.8 
    '11/29/1996'    179.3 
    '12/30/1996'    190.4 
    '1/31/1997'     185.7 
    

    In windows, with Excel, the variable Data is a string field of the cell array. To convert it just do:

    T.Data = datetime (T.Data, 'InputFormat', 'MM / dd / yyyy')

    T =

       Data        Peso
    ___________    ______
    
    31-Oct-1996    174.8 
    29-Nov-1996    179.3 
    30-Dec-1996    190.4 
    31-Jan-1997    185.7 
    

    This example was originally put into when-to -convert-dates-from-excel-files

        
    09.10.2014 / 16:48