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.

 Réponse acceptée

Stephen23
Stephen23 le 14 Oct 2015
Modifié(e) : Stephen23 le 14 Oct 2015

0 votes

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

Plus de réponses (2)

Walter Roberson
Walter Roberson le 14 Oct 2015

0 votes

You will need to use datetime objects or duration objects, which were introduced in R2014b.
Jan
Jan le 14 Oct 2015

0 votes

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

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by