Tae-Sung Shin
Tae-Sung Shin

Reputation: 20633

How to convert comma-separated string to cell array of strings in 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?

Upvotes: 4

Views: 11144

Answers (3)

frankli22586
frankli22586

Reputation: 740

a more simple way: t1 = strsplit('A, B, C, D', ',');

Upvotes: 3

Jonas
Jonas

Reputation: 74930

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'

Upvotes: 5

H.Muster
H.Muster

Reputation: 9317

For your specific example try:

cellstr(strread('A, B, C, D','%c,'))'

Upvotes: 3

Related Questions