Remove specific characters from cell array

27 views (last 30 days)
Luis Lopez
Luis Lopez on 24 Oct 2018
Answered: Luis Lopez on 25 Oct 2018
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

Answers (3)

Guillaume
Guillaume on 24 Oct 2018
  • 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 on 25 Oct 2018
If string before ':' is always two digit, the following will work.
output = regexprep(yourCellArray,'\d{2}:','');

Luis Lopez
Luis Lopez on 25 Oct 2018
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.

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!