Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
extracting elements from a cell array

Subject: extracting elements from a cell array

From: Daragh

Date: 27 Oct, 2010 16:20:05

Message: 1 of 7

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!

 

Subject: extracting elements from a cell array

From: Sean

Date: 27 Oct, 2010 17:16:05

Message: 2 of 7

"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

Subject: extracting elements from a cell array

From: Sean

Date: 27 Oct, 2010 18:14:04

Message: 3 of 7


Typo here should be:

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

Subject: extracting elements from a cell array

From: Jos (10584)

Date: 27 Oct, 2010 18:35:05

Message: 4 of 7

"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

Subject: extracting elements from a cell array

From: Daragh

Date: 28 Oct, 2010 15:39:05

Message: 5 of 7

"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

Subject: extracting elements from a cell array

From: Jan Simon

Date: 28 Oct, 2010 15:40:05

Message: 6 of 7

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

Subject: extracting elements from a cell array

From: Daragh

Date: 28 Oct, 2010 15:56:04

Message: 7 of 7

"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;

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us