Azzera filtri
Azzera filtri

Efficient splitting of strings

5 visualizzazioni (ultimi 30 giorni)
Hamad Alsayed
Hamad Alsayed il 21 Mar 2016
Commentato: Hamad Alsayed il 21 Mar 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).

Risposta accettata

KSSV
KSSV il 21 Mar 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 Commento
Hamad Alsayed
Hamad Alsayed il 21 Mar 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Characters and Strings in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by