Remove specific characters from cell array

조회 수: 28 (최근 30일)
Luis Lopez
Luis Lopez 2018년 10월 24일
답변: Luis Lopez 2018년 10월 25일
Hi,
Maybe exist an easy way to do what I'm looking for. I want to create a new cell array from a current cell array but including only characters from positions 2 to 5 for example. Some elements in cell array are empty. Will be also helpfull if I could get a way to obtain 4 characters after ":" it, will be great.
Thanks for your help

답변 (3개)

Guillaume
Guillaume 2018년 10월 24일
  • extract elements 2 to 5:
cellfun(@(x) x(2:min(end, 5)), yourcellarray, 'UniformOutput', false)
the min(end, 5) is to make sure it doesn't error if the content of a cell is less than 5 elements.
  • extract 4 characters after :, only works if all elements of the cell array are char array (empty char arrays are fine):
regexp(yourcellarray, '(?<=:)....', 'match', 'once')

Akira Agata
Akira Agata 2018년 10월 25일
If string before ':' is always two digit, the following will work.
output = regexprep(yourCellArray,'\d{2}:','');

Luis Lopez
Luis Lopez 2018년 10월 25일
Hi,
Both answers work fine for my initial question. Now I'm wondering if you can help me to identify a way to create n variables from a cell array.
Where my variable B has many elements to create new variables, alwas 4 digits after ":". My desired outputs is in this case a logical variable called "F_3749" and the second one "F_1671".
Thanks for your help.

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by