MATLAB Answers


converting cells with strings inside cells into strings inside cells

Asked by Micha
on 24 Jul 2018
Latest activity Edited by Micha
on 24 Jul 2018
I use the following code: cellfun(@(x) regexp(x, '@(.*)@', 'tokens'), array_of_strings) The idea is to replace all the strings in the array with only the substring between the @'s. It works, the only problem is that the 'tokens' option leaves me with cells inside cells, which is inconvenient. My questions are: 1. Is there an alternative way to do it without getting cells inside cells? 2. It is interesting for me to know if there is a function that converts "cell arrays with strings inside a cell array" into simply "strings in a cell array". Thanks


Can you show a sample of array_of_strings?
array_of_strings = {'a @one@', 'a @two@', 'a@three@'};

Sign in to comment.




1 Answer

Answer by Paolo
on 24 Jul 2018
Edited by Paolo
on 24 Jul 2018
 Accepted Answer

cellfun(@(x) regexp(x,'(?<=@)(.*)(?=@)','match'),array_of_strings)
cellfun(@(x) regexp(x,'(?<=@)(.*)(?=@)','tokens','once'),array_of_strings)


Of course, your 2 solutions solved this problem to begin with. But I was interested if there is any function to "fix" this new condition.
{'one'} {'two'} {'three'}
Does this help?
That seems to do the trick

Sign in to comment.