Remove specific characters from cell array

40 次查看(过去 30 天)
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