1

I am trying to read a test table from an excel sheet , my excel sheet looks something like this

 Building      location start_date  start_time  end_date    end_time    number
 Test_Building  1.08    12.06.2016  19:00       12.06.2016      19:30    7

now when i read this as a table in matlab by using :

R = readtable('testtable.xlsx','ReadVariableNames',1) 

I get something like

R = 

       Building        location     start_date     start_time      end_date      end_time    number
    _______________    ________    ____________    ____________    ____________    ________    ______

    'Test_Building'    1.08        '12.06.2016'    0.79167         '12.06.2016'    0.8125      7     

Now if you notice the start_time and end_time are retrieved in datenum format instead of a datestrhow can I save this time such that when I retrieved these variables they are in datestr format ?

Novice_Developer
  • 1,432
  • 2
  • 19
  • 33

1 Answers1

1

The key thing to realize here is that if you use a date in Excel, it will not be stored as a string, but as the corresponding datenum. In particular, two people who open the same file may see a different string based on their settings.

As such there is no such thing as 'getting the date string from excel'.

There are workarounds for this, for instance by storing your date in excel as a string (as you mentioned in the comments). However, this is not always possible.

Perhaps there is even a possibility to load the string as Excel would display it given your current settings, but this is of course not very portable, and if you change your settings you could easily get in trouble because the following calculations in Matlab will not match the format anymore. Not to mention that you would need to parse date columns with different formats individually, even if they are in the same file.

As such, I recommend the following:

Read the datenum, and parse it in MATLAB

Of course you may want to a date, but even if you parse it to a string this would still be the recommended general solution.

You mention in the comments that you want to use datevec, therefore you should be able to use one of these two options:

DateVector = datevec(DateNumber)

Depending on your source you may need to add or substract a constant from the number to get the right date. (Probably this is not needed for Excel, but just try it once.)

Community
  • 1
  • 1
Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
  • Thankyou so much for the detailed reply , your solution works! The reason I couldnt use `datevec` directly was that these excel plots were being retrieved by a function which read this time as a `datetime(TMP.start_time,'InputFormat','HH:mm:ss')`where you can see the input format was `HH:mm:ss` , I didnt wanted to change the function as it was working for other excel files also ... Thanks again for the help – Novice_Developer Aug 16 '16 at 10:09