Remove specific characters from cell array

42 ビュー (過去 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 Center および File ExchangeData Type Conversion についてさらに検索

タグ

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by