Search code examples
matlabdatesplitcell-array

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.

Solution

  • 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));