필터 지우기
필터 지우기

Can strsplit be applied to a matrix?

조회 수: 4 (최근 30일)
TingTing
TingTing 2015년 8월 10일
댓글: Walter Roberson 2015년 8월 13일
Hi, I am used strsplit to take out 1 from string of "1: abc". I have to loop each string and that takes time. Is there anyway that I can apply this function on a column or a matrix? Thanks!

답변 (1개)

Walter Roberson
Walter Roberson 2015년 8월 10일
Have you considered using regexp() or regexprep() ?
  댓글 수: 2
TingTing
TingTing 2015년 8월 12일
Hi, thanks for tip! However, I don't really get it. strsplit does the job, though it has to loop. I would like to have something that applies to the whole column without looping. I am not sure if regexp works, and even if that works, it still works per string, doesn't it?
Walter Roberson
Walter Roberson 2015년 8월 13일
Is your data a char array? If so then just access columns 2 onward
without_leading_digit = TheData(:,2:end);
Or is it only to be removed if it is '1' but not if it is a different digit? Or is it only to be removed if it is a series of digits followed by a colon? Is the colon to be left in place? Is the space to be left in place? If you are using a char array then what is the expected result if the resulting strings would be a different size? If you are using a char array then would it be acceptable for trailing spaces to be deleted along the way?
If you are using a cell array of strings, then if you are removing a fixed number of characters remember there is always cellfun
without_leading_digit = cellfun(@(S) S(2:end), TheDataCell, 'Uniform', 0);
Did you consider reading the documentation of regexp() and regexprep?
If you want the input split at the colon then do you want both parts or do you want everything after the colon or everything after the space after the colon?
>> t = {'1: zip', '23: frodo'}; regexprep(t, '[^:]+:\s+', '')
ans =
'zip' 'frodo'

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

카테고리

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