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.

 Respuesta aceptada

Stephen23
Stephen23 el 14 de Oct. de 2015
Editada: Stephen23 el 14 de Oct. de 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

Más respuestas (2)

Walter Roberson
Walter Roberson el 14 de Oct. de 2015

0 votos

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

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 14 de Oct. de 2015

Editada:

el 14 de Oct. de 2015

Community Treasure Hunt

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

Start Hunting!

Translated by