Remove specific characters from cell array

42 visualizaciones (últimos 30 días)
Luis Lopez
Luis Lopez el 24 de Oct. de 2018
Respondida: Luis Lopez el 25 de Oct. de 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

Respuestas (3)

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

Luis Lopez
Luis Lopez el 25 de Oct. de 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.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by