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:
Search cellarray

Subject: Search cellarray

From: Robin Jens

Date: 31 Jan, 2011 21:53:03

Message: 1 of 5

Hi

I have a cellarray that contain 3 columns with data.

cellArray(:,1)={1;2;3;4}
cellArray(:,2)={'Beer 1';'Beer 2';'Beer 3';'Beer 4'}
cellArray(:,3)={'Carlsberg';'Heineken';'Newcastle';'Newcastle'}

I want a function that finds the rows that contain some "key" values in column 3 and finds the values at the same rows in column 1 and 2.
For example I want my function to find the rows that contain the keys "Carlsberg" and "Heineken" my output function should write:

output= {1 'Beer 1';2 'Beer 2'}
If easier it could also write the output in 2 cellarrays:
output1={1 ;2}
output2={'Beer 1' ;'Beer 2'}


I know how to search for one "key" in a cellarray, but not how to add the key into the output cellarray.

output=find(ismember(cellArray, key)==1);

Best Regards

Robin Jens

Subject: Search cellarray

From: Oleg Komarov

Date: 31 Jan, 2011 22:24:03

Message: 2 of 5

"Robin Jens" wrote in message <ii7avv$mjl$1@fred.mathworks.com>...
> Hi
>
> I have a cellarray that contain 3 columns with data.
>
> cellArray(:,1)={1;2;3;4}
> cellArray(:,2)={'Beer 1';'Beer 2';'Beer 3';'Beer 4'}
> cellArray(:,3)={'Carlsberg';'Heineken';'Newcastle';'Newcastle'}
>
> I want a function that finds the rows that contain some "key" values in column 3 and finds the values at the same rows in column 1 and 2.
> For example I want my function to find the rows that contain the keys "Carlsberg" and "Heineken" my output function should write:
>
> output= {1 'Beer 1';2 'Beer 2'}
> If easier it could also write the output in 2 cellarrays:
> output1={1 ;2}
> output2={'Beer 1' ;'Beer 2'}
>
>
> I know how to search for one "key" in a cellarray, but not how to add the key into the output cellarray.
>
> output=find(ismember(cellArray, key)==1);
>
> Best Regards
>
> Robin Jens


% Single Output
Out = cellArray(strcmp(cellArray(:,3),'Newcastle'),[1,2]);

% Distribute over two out
[Out1{1:2,1} Out2{1:2,1}] = deal(cellArray{strcmp(cellArray(:,3),'Newcastle'),[1,2]});

Oleg

Subject: Search cellarray

From: Matt Fig

Date: 1 Feb, 2011 00:06:04

Message: 3 of 5

Are you still working on this old problem? I thought it looked familiar...



http://www.mathworks.com/matlabcentral/newsreader/view_thread/275117

Subject: Search cellarray

From: Jan Simon

Date: 1 Feb, 2011 14:24:04

Message: 4 of 5

Dear Matt Fig,

> Are you still working on this old problem? I thought it looked familiar...
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/275117

Interesting. And except for the names "Jens" and "Robin Jens" the OP asked a question as "Hans" some weeks before:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/273758#718769
At first I thought of KI experiments concerning learning or communication. Then I tried to get money from Heineken for mentioning the trademark in a forum, but no success.

@Jens: Again: This is not meant personally. I'm just curious.

Kind regards, Jan

Subject: Search cellarray

From: Robin Jens

Date: 1 Feb, 2011 21:47:04

Message: 5 of 5

"Jan Simon" wrote in message <ii9524$q0s$1@fred.mathworks.com>...
> Dear Matt Fig,
>
> > Are you still working on this old problem? I thought it looked familiar...
> > http://www.mathworks.com/matlabcentral/newsreader/view_thread/275117
>
> Interesting. And except for the names "Jens" and "Robin Jens" the OP asked a question as "Hans" some weeks before:
> http://www.mathworks.com/matlabcentral/newsreader/view_thread/273758#718769
> At first I thought of KI experiments concerning learning or communication. Then I tried to get money from Heineken for mentioning the trademark in a forum, but no success.
>
> @Jens: Again: This is not meant personally. I'm just curious.
>
> Kind regards, Jan


Dear Jan,Matt and Oleg

To make a long story short I forgot my password to my email address and therefore I could not get access to my password for my mathworks account.That is why I needed to create a new profil here at mathworks forum. I greatly appreciate all your help. Hope I some day can help new matlab users as you have done for years. By the way I sovled my problem by doing what Oleg suggested:

Out1 = cellArray(strcmp(cellArray(:,3),'Heineken'),[1,2]);
Out 2= cellArray(strcmp(cellArray(:,3),'Carlsberg'),[1,2]);
Out=[Out1; Out2];

Jan: I prefer Carlsberg compared to Heineken.

Best Regards

Robin Jens

Tags for this Thread

No tags are associated with 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