How do I split cell array of strings by using a character delimiter
371 views (last 30 days)
Show older comments
MathWorks Support Team
on 6 Apr 2020
Answered: MathWorks Support Team
on 6 Apr 2020
I have a one-column text array, which looks like the following:
AAAS|8086
ABAT|18
ACSM5|54988
C1orf64|149563
LOC100009676|100009676
RAB32|10981
...
This array comprises > 30,000 cells, and the composition of each cell is as follows: there is a string of alphanumeric characters before the vertical slash (|) and a string of numeric characters after the vertical slash. I would like to split this one-column array into two separate columns such that the first column contains only the alphanumeric characters before the vertical slash and the second column contains only the numeric characters after the vertical slash. I also do not want the vertical slash to be included in either of the columns.
Accepted Answer
MathWorks Support Team
on 6 Apr 2020
Use the 'strsplit' function to split a string by specifying the '|' character as a delimiter. We can also use the 'cellfun' function to repeat the 'strsplit' function on each cell of a cell array. The code below demonstrates the use of the two functions to achieve the output you require (A is the initial one column cell array).
>> newA = cellfun(@(x) strsplit(x, '|'), A, 'UniformOutput', false);
>> newA = vertcat(newA{:}); % To remove nesting of cell array newA
For more information regarding the use of the above mentioned function, please refer to the documentation links below:
0 Comments
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!