Convert Cell Arrays to Doubles

How can I convert a cell array of times:
C = {'12:35' '11:35'
'14:21' '11:01'}
to an array of doubles
B = 12:35 11:35
14:21 11:01
where I can do mathematical operations with it if given a certain amount of minutes.
For example, 12:35 + 60 minutes = 13:35.

 Accepted Answer

Stephen23
Stephen23 on 14 Oct 2015
Edited: Stephen23 on 14 Oct 2015
The output that you request is not possible, because the colon character : cannot be part of a double array. If you really want those numbers in a numeric array then that array will need to be a different size to the input cell array. Here is one easy conversion that puts the hours on the top row and the minutes underneath:
>> C = {'12:35','11:35';'14:21','11:01'};
>> M = sscanf(char(C)','%2d:%2d',[2,numel(C)])
M =
12 14 11 11
35 21 35 1
Of course if you really want those values in exactly the same shape array as the input cell array C, then the minutes can be converted to decimal fractions of the hours:
>> N = reshape(M(1,:)+M(2,:)/60,size(C))
N =
12.583 11.583
14.350 11.017

More Answers (2)

Walter Roberson
Walter Roberson on 14 Oct 2015
You will need to use datetime objects or duration objects, which were introduced in R2014b.
C = {'12:35' '11:35'
'14:21' '11:01'}
D = datenum(C, 'HH:MM')

Categories

Asked:

on 14 Oct 2015

Edited:

on 14 Oct 2015

Community Treasure Hunt

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

Start Hunting!