Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Processing Cell Array of Cell Array of Strings

Subject: Processing Cell Array of Cell Array of Strings

From: omegayen

Date: 10 Oct, 2012 22:01:08

Message: 1 of 3

Hi,

So I have a cell array of a cell array of strings that I am working with.

As an example consider the following:

test=cell(5,1);
test{1,1}{1}=num2str(1);
test{1,1}{2}=num2str(0);
test{2,1}{1}=num2str(0);
test{2,1}{2}=num2str(0);
test{3,1}{1}=num2str(1);
test{3,1}{2}=num2str(1);
test{4,1}{1}=num2str(0);
test{4,1}{2}=num2str(0);
test{4,1}{3}=num2str(0);
test{5,1}{1}=num2str(0);
test{5,1}{2}=num2str(1);
test{5,1}{3}=num2str(0);

I want to process this cell array of cell array of strings so that the inside cell array is removed to make it easier to analyze and look at the results.

Hence I can use the following code to do this and separate the strings in each row of the outer cell array with commas

test2=cellfun(@(x){strcat(x,',')},test)
test2=cellfun(@(x) {deblank([x{:}])},test2);

However, when I do this I get an extra comma (,) at the end. Does anyone know how I can do something similar but not have this extra comma (,) at the end of each row of test2 in this case?

Note: disregard that the strings are 1's and 0's in the sample as they can also be actual alphabet characters

Subject: Processing Cell Array of Cell Array of Strings

From: Kristin

Date: 12 Oct, 2012 05:12:10

Message: 2 of 3

"omegayen " <omegayen@ameritech.net> wrote in message <k54r74$6i9$1@newscl01ah.mathworks.com>...
> Hi,
>
> So I have a cell array of a cell array of strings that I am working with.
>
> As an example consider the following:
>
> test=cell(5,1);
> test{1,1}{1}=num2str(1);
> test{1,1}{2}=num2str(0);
> test{2,1}{1}=num2str(0);
> test{2,1}{2}=num2str(0);
> test{3,1}{1}=num2str(1);
> test{3,1}{2}=num2str(1);
> test{4,1}{1}=num2str(0);
> test{4,1}{2}=num2str(0);
> test{4,1}{3}=num2str(0);
> test{5,1}{1}=num2str(0);
> test{5,1}{2}=num2str(1);
> test{5,1}{3}=num2str(0);
>
> I want to process this cell array of cell array of strings so that the inside cell array is removed to make it easier to analyze and look at the results.
>
> Hence I can use the following code to do this and separate the strings in each row of the outer cell array with commas
>
> test2=cellfun(@(x){strcat(x,',')},test)
> test2=cellfun(@(x) {deblank([x{:}])},test2);
>
> However, when I do this I get an extra comma (,) at the end. Does anyone know how I can do something similar but not have this extra comma (,) at the end of each row of test2 in this case?
>
> Note: disregard that the strings are 1's and 0's in the sample as they can also be actual alphabet characters

example:
"cellstr" is a cell array of cell array of strings
cellstr = { {'why hello there'}; {'matlab is fun}; {'12345abc'}};
newcellstr = [cellstr{:}];

Then newcellstr is a cell array of strings, like:
newcellstr = {'why hello there'; 'matlab is fun; '12345abc'};

The key is in this bracket notation.
If you just type cellstr{:} into the command prompt, you will see the contents of the cells, but as three different outputs... Putting the square brackets around this remedies the situation. Hope this helps!

Subject: Processing Cell Array of Cell Array of Strings

From: Bruno Luong

Date: 12 Oct, 2012 05:43:09

Message: 3 of 3

"omegayen " <omegayen@ameritech.net> wrote in message <k54r74$6i9$1@newscl01ah.mathworks.com>...

>
> However, when I do this I get an extra comma (,) at the end. Does anyone know how I can do something similar but not have this extra comma (,) at the end of each row of test2 in this case?

Simply delete the last character:

test2=cellfun(@(x) {x(1:end-1)}, cellfun(@(x) {sprintf('%s,',x{:})},test))

% Bruno

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us