Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert comma-separated string to cell array of strings in matlab

Tags:

matlab

I have following string

'A, B, C, D'

from which I want to make a cell array such as

{ 'A', 'B', 'C', 'D' }

How would I be able to do this in Matlab?

like image 559
Tae-Sung Shin Avatar asked Dec 20 '22 22:12

Tae-Sung Shin


1 Answers

Here's a solution that will cut up the string at commas, semicolons, or white spaces, and that will work for strings of any length

string = 'A, BB, C'

tmp = regexp(string,'([^ ,:]*)','tokens');
out = cat(2,tmp{:})


out = 

    'A'    'BB'    'C'
like image 146
Jonas Avatar answered May 24 '23 04:05

Jonas