How to delete and change string in cell array?

조회 수: 1 (최근 30일)
Sara Antonio
Sara Antonio 2016년 6월 6일
편집: Azzi Abdelmalek 2016년 6월 6일
Hi, I have a cell array with every cell containing a string for every day such as: 1313406900.Mon.Aug.15_11_15_00.GMT.2011.nordzee1.cx.plan.bar
I want to change the strings on my cell array so it only give me the date, as Aug-15-2011 on the example. How can I delete the part of the string I'm not interested in?
Thanks in advance, Sara

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2016년 6월 6일
편집: Azzi Abdelmalek 2016년 6월 6일
s={'1313406900.Mon.Aug.15_11_15_00.GMT.2011.nordzee1.cx.plan.bar';'1313406900.Mon.Jun.17_12_15_00.GMT.2011.nordzee1.cx.plan.bar'}
out=datestr(regexpi(s,'(?<=[a-z]+\.)([a-z]+\.\d+_\d+)','match','once'),'dd-mmm-yyyy')

Guillaume
Guillaume 2016년 6월 6일
On the assumption that the first _ is always between the day and the year, and that the year is always 20xx, and that the month is always before a dot immediately before the day:
regexprep(yourcellarray, '.*?([^.]+)\.(\d+)_(\d+).*', '$1-$2-20$3')
will work. The regular expression has five parts:
  • .*? is intended to match everything before the month. It matches any number of characters but as few as possible so that the rest of the expression still matches
  • ([^.]+)\. is intended to capture the month. It matches the longest sequence of non-dot characters followed by a dot. The sequence of non-dot character is token number 1
  • (\d+)_ is intended to capture the day. It matches as many digits as possible followed by a _. The digit sequence is token number 2
  • (\d+) is intended to capture the year. It matches as many digits as possible. This is token number 3
  • .* matches the remainder of the string
Tokens 1,2 and 3 are simply strung together with a - in between and a 20 before token 3. The rest of the match is discarded.

카테고리

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