Calculate hours elapsed from 2 times

Hello,
I have a cell array with two columns. It looks like the following:
thetime = ['9:30PM' , '12:30pm', '11:30am' ; '8:00am', '6-6:30am', '7:20am']
I want to find out how many hours have passed from items in column 1 to those in column 2 (same row).
Before subtracting starting time from ending time, I have tried to convert am/pm times to 24hour-time using:
hoursspent = datestr([thetime{:}],'HH:MM');
but this returns: Cannot convert input into specified date string. DATENUM failed.
I have also tried:
hoursspent = datenum([thetime{:}],'HH:MM');
but this returns a 1x1 cell with the number 7.3487e+05
What am I doing wrong? Any ideas as to what I can try instead?
Thank you so much for your time and help!

답변 (2개)

Sumit Tandon
Sumit Tandon 2012년 7월 25일

0 개 추천

Try using the DATEVEC command to find difference between two dates/times.
For example:
>> a = datevec('9:30 PM');
>> b = datevec('9:40 PM');
>> b-a
ans =
0 0 0 0 10 0
DATENUM converts date information to serial date format.

댓글 수: 5

Milou
Milou 2012년 7월 25일
When I tried that I got the following:
Error using datevec (line 213) Failed to lookup month of year.
is this because I only have times in my data?
Sumit Tandon
Sumit Tandon 2012년 7월 25일
What did you specify as input? DATEVEC should assume default year, month and day if only time is specified.
Milou
Milou 2012년 7월 25일
i specified the columns (1 and 3) with time data in them.
hours1 = data(:,1);
hours3 = data(:,3);
hours1 = datevec(hours1);
hours3 = datevec(hours3);
AmountTime = hours1 - hours3;
Sumit Tandon
Sumit Tandon 2012년 7월 25일
Are you sure about the data going into DATEVEC command? Use the debugger to check whats in hours1 and hours3. DATEVEC does work with cell arrays if you are using that.
Note that variable "thetime" in your original question is a normal char array and not a cell array. Use {} for cell arrays.
Milou
Milou 2012년 7월 25일
also, i just realized that your example above shows:
9:40pm - 9:30pm = 10.
I want a function that calculates the time elapsed between those times, ie. 12 hours.
Is there any way to do this?

댓글을 달려면 로그인하십시오.

Star Strider
Star Strider 2012년 7월 25일

0 개 추천

See the ‘etime’ function.
Example:
thetime = {'9:30PM' , '12:30pm', '11:30am' ; '8:00am', '6-6:30am', '7:20am'}'
time2 = datevec(thetime(1,:), 16)
Td_s = etime(time2(1,:),time2(2,:)) % Elapsed time in seconds
Td_h = Td_s/3600 % Elapsed time in hours
The ‘16’ is a predefined time format. See ‘Numeric Identifiers for Predefined Formats’ about half way down the ‘Date and Time Functions’ page: http://www.mathworks.com/help/techdoc/matlab_prog/bspgcx2-1.html

댓글 수: 7

Milou
Milou 2012년 7월 25일
when i try that, the output is a 1x1 cell array, so only one number.
does this mean that only two items (one in each) is evaluated?
do you know how i can resolve this?
thank you for your time.
Star Strider
Star Strider 2012년 7월 25일
편집: Star Strider 2012년 7월 25일
*NOTE:* I did my best to format the Command Window output here as ‘code’ but while it displayed correctly in ‘Preview’ it doesn't seem to have carried over to the posted version after I clicked ‘Save’.
__________________________________________________________________________
My pleasure.
When you say ‘output’, what is the output you refer to?
When I ran the code I quoted, I got these results:
thetime =
'9:30PM' '8:00am'
'12:30pm' '6-6:30am'
'11:30am' '7:20am'
time2 =
Columns 1 through 5
2.0120e+003 1.0000e+000 1.0000e+000 21.0000e+000 30.0000e+000
2.0120e+003 1.0000e+000 1.0000e+000 8.0000e+000 0.0000e+000
Column 6
0.0000e+000
0.0000e+000
Td_s =
48.6000e+003
Td_h =
13.5000e+000
What part of my code fragment didn't work for you?
Please note that I intend it as an example. You will have to adapt it to your code, particularly because I have no idea what you want to do with '6-6:30am'. The MATLAB functions will not understand what you mean with that so you will have to change it to something ‘datevec’ will understand before you run your code.
Milou
Milou 2012년 7월 26일
i formatted to fit my code: it works but only for the first item (first row of adjacent columns 1 and 2), i want a function that gives me the time difference for all adjacent elements of the array (2 columns of 10 elements each, so i am trying to find a function that goes through all of them and returns 10 differences).
It is probably best in your situation to use a ‘for’ loop. (I am currently running a very large ‘GlobalSearch’ problem in the background in MATLAB that will probably run for several more hours, so I am not able to test the code.)
I suggest you consider something like:
--------------------------------------------------------
for k1 = 1:size(thetime,1)
time2 = datevec(thetime(k1,:), 16);
Td_s = etime(time2(1,:),time2(2,:));
Td_h(k1) = Td_s/3600;
end
--------------------------------------------------------
I put extra blank lines in my loop and lines around my code in case it again refuses to format as code.
You still need to deal with the '6-6:30am' problem. As it is now, the ‘datevec’ function will throw an error when it gets to that row.
The ‘datevec’ function assumes the current year, month, and day, and that the times reflect consecutive days. To be accurate, you need to provide the year, month, and day for each time as well.
Milou
Milou 2012년 7월 26일
even with the loop and changing '6-6:30am' to '6:30am', i get the following error:
Error using datevec (line 213) Failed to lookup month of year.
where do i provide the year, month, and day for each time?
Star Strider
Star Strider 2012년 7월 26일
How far into the loop do you get your error? Does it read most of your times?
When I ran my code (on MATLAB 2012a), it assumed the current year, month, and day, then used your time data, as in the Command Window output I quoted earlier (but couldn't format correctly). According to the documentation I quoted to you in my original Anwwer, ‘datevec’ does not require any more than the time. That is why I used the pre-defined format ‘16’.
Since I cannot reproduce your error in my code (in part because my GlobalSearch problem is still running 13½ hours after I started it and I cannot test anything until it finishes), I do not know what to advise you to do.
After stopping GlobalSearch (after 18 hours without results) I ran the code I gave you with ‘6-6:30pm’ changed to ‘6:30pm’. After changing this line to guarantee a column vector:
Td_h(k1,:) = Td_s/3600;
got this output:
Td_h =
13.5000e+000
6.0000e+000
4.1667e+000
Since I cannot reproduce your error, I cannot help you figure out what is wrong with your code and why my code won't run in your application.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Dates and Time에 대해 자세히 알아보기

질문:

2012년 7월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by