필터 지우기
필터 지우기

Efficient splitting of strings

조회 수: 4 (최근 30일)
Hamad Alsayed
Hamad Alsayed 2016년 3월 21일
댓글: Hamad Alsayed 2016년 3월 21일
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).

채택된 답변

KSSV
KSSV 2016년 3월 21일
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
Hamad Alsayed
Hamad Alsayed 2016년 3월 21일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by