COMPGROUPS.NET | Search | Post Question | Groups | Stream | About | Register

### extracting elements from a cell array

• Email
• Follow

```Hi,

I have a large cell array of times:

'08:30:01'
'08:30:02'

etc...

I'd like to convert the times above to seconds. To do this I loop through each element
extracting the hour "08", minutes "30" and seconds "01" and then
using str2double to convert this to an integer number of seconds.
It works but is VERY slow. Is there a quicker way? Maybe its possible to do this without
using loops?

Thanks!

```
 0

See related articles to this posting

```"Daragh " <wawel100@gmail.com> wrote in message <ia9jfl\$6bv\$1@fred.mathworks.com>...
> Hi,
>
> I have a large cell array of times:
>
> '08:30:01'
> '08:30:02'
>
> etc...
>
> I'd like to convert the times above to seconds. To do this I loop through each element
> extracting the hour "08", minutes "30" and seconds "01" and then
> using str2double to convert this to an integer number of seconds.
> It works but is VERY slow. Is there a quicker way? Maybe its possible to do this without
> using loops?
>
> Thanks!

doc cellfun

Then use an anonymous function for the calculation
secfun = @(x)str2double(A(1:2))*3600+str2double(A(4:5))*60+str2double(A(7:8));

seconds = cellfun(@secfun,your_cell);

%Sean
```
 0

```Typo here should be:

secfun = @(x)str2double(x(1:2))*3600+str2double(x(4:5))*60+str2double(x(7:8));
```
 0

```"Daragh " <wawel100@gmail.com> wrote in message <ia9jfl\$6bv\$1@fred.mathworks.com>...
> Hi,
>
> I have a large cell array of times:
>
> '08:30:01'
> '08:30:02'
>
> etc...
>
> I'd like to convert the times above to seconds. To do this I loop through each element
> extracting the hour "08", minutes "30" and seconds "01" and then
> using str2double to convert this to an integer number of seconds.
> It works but is VERY slow. Is there a quicker way? Maybe its possible to do this without
> using loops?
>
> Thanks!
>
>

You can use a the various date and time functions of Matlab

For example:

t = {'08:30:01', '08:30:02'}
t_in_seconds = etime(datevec(t),datevec(datestr(t,1)))

hth
Jos
```
 0

```"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <ia9q5c\$t8p\$1@fred.mathworks.com>...
>
> Typo here should be:
>
> secfun = @(x)str2double(x(1:2))*3600+str2double(x(4:5))*60+str2double(x(7:8));

Thanks Sean,

Its an elegant solution that works but unfortunately its slow. Is there a quicker way?

Daragh
```
 0

```Dear Daragh!

C = {'08:30:01', '08:30:02'};
catC = sprintf('%s:', C{:});
D = sscanf(catC, '%d:', [3, Inf]);
Sec = [3600, 60, 1] * D;  % Matrix multiplication

If C is large, the concatenation is faster with the C-Mex CStr2String from the FEX.

Kind regards, Jan
```
 0

```"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <iac5gl\$p9e\$1@fred.mathworks.com>...
> Dear Daragh!
>
> C = {'08:30:01', '08:30:02'};
> catC = sprintf('%s:', C{:});
> D = sscanf(catC, '%d:', [3, Inf]);
> Sec = [3600, 60, 1] * D;  % Matrix multiplication
>
> If C is large, the concatenation is faster with the C-Mex CStr2String from the FEX.
>
> Kind regards, Jan

Thanks Jan,

I also tried:

[Y, M, D, H, MN, S] = datevec(C);
Seconds = 3600 * H + 60 * MN  + S;
```
 0