Replace string with another value

조회 수: 3 (최근 30일)
klb
klb 2020년 7월 4일
편집: klb 2020년 7월 4일
What would be a good way replace a full string with first 3 letters of last word. For example '13 Jon Walker' with 'WAL' .
I have about 25 such replacements an this is indeed doable line-by-line "by-hand" using replace function, but that beats the purpose of MATLAB. Plus i might have more in future. I looked up regexprep too but it doesnt quite do that. OR have I missed it ? Is there a way I could apply a 'replace full string with first 3 letters of last word ' rule?
Thank you for your time in advance.

채택된 답변

Walter Roberson
Walter Roberson 2020년 7월 4일
S = {'13 Jon Walker', '23 skidoo', '0 infinite way blvd'};
upper(regexprep(S, '^.*\s+(...).*', '$1'))
  댓글 수: 1
klb
klb 2020년 7월 4일
편집: klb 2020년 7월 4일
Thank you this works! Could you please explain the meaning of qualifiers though.
Could you also clarify this if possible:
replace(13 Jon Walker,'WAL') returns '13 Jon Walker' not 'WAL' But
replace(13 Jon Walker,'WAL') returns 'WAL' .
Why would that be?

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

추가 답변 (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