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:
Removing rows from a cell array

Subject: Removing rows from a cell array

From: Kevin Ellis

Date: 4 Dec, 2012 00:02:29

Message: 1 of 2

Hello,

I am having trouble removing rows from a cell array. I have a much larger cell array, but a portion of it is shown as follows:

ans =
    '11' '111213424137S' '6/7/2012 7:50:01 AM' [150.6] [2000.58]
    '11' '111213424137S' '6/14/2012 11:14:07 AM' [1407.34] [0]
    '11' '111213424141S' '6/7/2012 7:58:06 AM' [-134.45] [134.45]
    '11' '111213424141S' '6/14/2012 11:10:08 AM' [200.08] [0]

Now I want to remove the rows where a zero appears in the fifth column of the cell array. In this case I want to remove rows 2 and 4. I have tried using a for loop to accomplish this, but the dimensions of the matrix always change. I have tried using cellfun also,

MasterSAS(any(cellfun(@(x) x(5) == 0, MasterSAS),2), :) = [];

But this clearly does not work. Any clarification or help would be much appreciated. Thanks.

Kevin

Subject: Removing rows from a cell array

From: Jos (10584)

Date: 4 Dec, 2012 10:49:08

Message: 2 of 2

"Kevin Ellis" wrote in message <k9jeil$gc5$1@newscl01ah.mathworks.com>...
> Hello,
>
> I am having trouble removing rows from a cell array. I have a much larger cell array, but a portion of it is shown as follows:
>
> ans =
> '11' '111213424137S' '6/7/2012 7:50:01 AM' [150.6] [2000.58]
> '11' '111213424137S' '6/14/2012 11:14:07 AM' [1407.34] [0]
> '11' '111213424141S' '6/7/2012 7:58:06 AM' [-134.45] [134.45]
> '11' '111213424141S' '6/14/2012 11:10:08 AM' [200.08] [0]
>
> Now I want to remove the rows where a zero appears in the fifth column of the cell array. In this case I want to remove rows 2 and 4. I have tried using a for loop to accomplish this, but the dimensions of the matrix always change. I have tried using cellfun also,
>
> MasterSAS(any(cellfun(@(x) x(5) == 0, MasterSAS),2), :) = [];
>
> But this clearly does not work. Any clarification or help would be much appreciated. Thanks.
>
> Kevin

Your mention of "the dimensions of the matrix always change" worries me a little. What matrix are you refering to?

You can concatenate the last column of the cell array C (if there is always a scalar in there!).

c5 = cat(1,C{:,end}) ;
q = c5 ~= 0 ;
D = C(q,:) ;

This will fail if the last column is not a scalar or empty.

hth

~ Jos

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