from Excel Time Import and Use in Arithmetically in Matlab

2 views (last 30 days)
Hi,
I have a problem about time values in Matlab. I prepared excel sheet which contains times. I set the cell format hh:mm:ss in Excel and write the times for example 10:10:00. In another cell, I write 00:08:00 after that I want to subtraction 00:08:00 from 10:10:00. Normally, I want to get 10:02:00. But, after make xlsread and datestr, it makes the matrix in char, but I dont want it. I want the matrix m*n like in Excel. but, after datestr all times is written in 1 column in char type in matlab. I want to make my calculations like this for example, after xlsread or another method, t1=a(1,1) then t2=a(2,3) and then x=t2-t1 and the result as 10:10:00 - 00:08:00 = 10:02:00. Is it possible?
Thank you.

Accepted Answer

Guillaume
Guillaume on 24 Feb 2018
Save yourself a lot of trouble if you are using an mildly recent version of matlab (R2014b or later) and use readtable instead of xlsread which will import your excel file more easily, and convert your time into modern datetime instead of the outdated datestr.
datetime knows how to calculate time differences properly.
data = readtable('yourexcelfile'); %usually it's all that is needed
timediff = diff(data.yourtimecolumn); %for example
  5 Comments
Peter Perkins
Peter Perkins on 26 Feb 2018
The times are being read in as Excel date numbers. 0.41667 is (a rounded version of) 10/24.
Convert to durations:
>> x = 10/24
x =
0.416666666666667
>> t = days(x); t.Format = 'hh:mm:ss'
t =
duration
10:00:00
Because of the inherit round-off in Excel's representation of time, you are likely to get some round-off creeping in. You might consider rounding those durations to the nearest second or whatever.
It's also possible that your file also has dates, in which case you probably want to combine date+time and use datetimes, not durations.

Sign in to comment.

More Answers (0)

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!