Austin
Austin

Reputation: 7319

string cell array split by delimiter to integer array

I have a cell array that is of size approximately 900k x 1 which contains dates in the format '5/13/2015 23:53'. I'm trying to create an integer array of the same length that contains just the hour in each date cell. What's the fastest/best way to do this?

Edit I only have access to the following toolboxes:

MATLAB                                                Version 8.6         (R2015b)
Simulink                                              Version 8.6         (R2015b)
Control System Toolbox                                Version 9.10        (R2015b)
DSP System Toolbox                                    Version 9.1         (R2015b)
Image Processing Toolbox                              Version 9.3         (R2015b)
Instrument Control Toolbox                            Version 3.8         (R2015b)
Optimization Toolbox                                  Version 7.3         (R2015b)
Signal Processing Toolbox                             Version 7.1         (R2015b)
Simulink Control Design                               Version 4.2.1       (R2015b)
Statistics and Machine Learning Toolbox               Version 10.1        (R2015b)
Symbolic Math Toolbox                                 Version 6.3         (R2015b)

Edit2:

tic
datesmat = datevec(Dates);
hours = datesmat(:,4);
toc

tic
Hours = cell2mat(cellfun(@(x) str2double(x(end-4:end-3)), Dates, 'UniformOutput', false));
toc

Elapsed time is 90.233473 seconds.
Elapsed time is 14.168023 seconds.

Upvotes: 2

Views: 116

Answers (3)

rahnema1
rahnema1

Reputation: 15837

A vectorized solution using logical indexing:

B = char(Dates).';
f = B ==':';
x=circshift(f,-1)|circshift(f,-2);
result = str2double(reshape(B(x),2,[]).');

Upvotes: 0

Adriaan
Adriaan

Reputation: 18177

date = {'5/13/2015 23:53';'5/13/2015 23:53'};
[~,~,~,hours,~,~] = datevec(date); % Extract only the hours

If you don't have the Finance toolbox, you can use a datevec for each date, whose fourth element will be the hour.

(I suspect the hour method of the Finance toolbox does something like this.)

Upvotes: 3

Rotem
Rotem

Reputation: 32084

You can use hour method.

Example:

Hour = hour({'5/13/2015 21:53', '5/13/2015 23:53'})

Result:

Hours =

    21    23

Example without financial toolbox:

Dates = {'5/13/2015 21:53', '5/13/2015 23:53'};

Hours = cell2mat(cellfun(@(x) str2double(x(end-4:end-3)), Dates, 'UniformOutput', false));

Upvotes: 3

Related Questions