Borrar filtros
Borrar filtros

Efficient splitting of strings

4 visualizaciones (últimos 30 días)
Hamad Alsayed
Hamad Alsayed el 21 de Mzo. de 2016
Comentada: Hamad Alsayed el 21 de Mzo. de 2016
I have a cell array of strings which I would like to split into sub-arrays as follows:
Suppose a date-time cell array reads as:
inputArray =
'20150316 07:20:39:380941'
'20150317 07:20:39:380941'
'20150318 07:20:39:380941'
'20150319 07:20:39:380941'
I would like the output to be
outputArray =
'20150316'
'20150317'
'20150318'
'20150319'
Currently I am doing it by
outputArray = cellfun(@(x) x(1:8),inputArray,'uni',false);
which works just fine, however for inputArray into the millions of elements, this process is slow.
Is there a more efficient method which can perform the above? I know for sure that the structure of the strings in the array is fixed (i.e. the string length is always 24 and the first 8 elements are YYYYMMDD).

Respuesta aceptada

KSSV
KSSV el 21 de Mzo. de 2016
You can read all the strings into a character matrices and extract what you want. Example:
k = ['20150316 07:20:39:380941'
'20150317 07:20:39:380941'
'20150318 07:20:39:380941'
'20150319 07:20:39:380941'];
k(:,1:9)
Have you tried this?
  1 comentario
Hamad Alsayed
Hamad Alsayed el 21 de Mzo. de 2016
That is perfect, Siva. Thank you. Even after "cellstr"-ing the answer back, execution time is cut by more than a half. Thanks again! Hamad.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Characters and Strings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by