Calculate hours elapsed from 2 times
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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!
0 Kommentare
Antworten (2)
Sumit Tandon
am 25 Jul. 2012
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 Kommentare
Sumit Tandon
am 25 Jul. 2012
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.
Star Strider
am 25 Jul. 2012
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 Kommentare
Star Strider
am 26 Jul. 2012
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.
Star Strider
am 26 Jul. 2012
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.
Siehe auch
Kategorien
Mehr zu Dates and Time finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!