Convert Cell Arrays to Doubles

1 view (last 30 days)
Dan Lynn
Dan Lynn on 14 Oct 2015
Edited: Stephen23 on 14 Oct 2015
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.

Jan
Jan on 14 Oct 2015
C = {'12:35' '11:35'
'14:21' '11:01'}
D = datenum(C, 'HH:MM')

Categories

Find more on Data Type Conversion 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!