tmo
tmo

Reputation: 1543

Matlab: Append unique strings into one cell with comma seperation

My input is N unique strings of different length stored in a structure such as

A.data{1} = {'The cat has'}
A.data{2} = {'green eyes'}

such that A.data is Nx1.

The desired output is a 1,1 cell with all the unique strings following each other and separated by commas.

output = ['The cat has' ', ' 'green eyes']

which produces The cat has, green eyes which is exactly what I want for my N strings.

Any ideas?

Thanks!

Upvotes: 0

Views: 87

Answers (1)

Luis Mendo
Luis Mendo

Reputation: 112689

Use strjoin:

A.data{1} = 'The cat has';
A.data{2} = 'green eyes';
result = strjoin(A.data, ', ');

gives

result =
The cat has, green eyes

If the data has an extra nesting level:

A.data{1} = {'The cat has'}
A.data{2} = {'green eyes'};

you need to get rid of it with

B = cellfun(@(x) x, A.data);

before calling strjoin:

result = strjoin(B, ', ');

Upvotes: 3

Related Questions