Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: comparison of cell entries

Subject: comparison of cell entries

From: Corinna Schmitt

Date: 08 Aug, 2007 11:45:02

Message: 1 of 4

Hallo,

I have a cell construction with 9 columns where the
following data is stored:

1 1 1 1 1 AR_WIZ
165855.0 59748.0 01_01_01_01
1 1 1 1 0 AR_WIZ
139876.0 60372.0 01_01_01_01
71662.0
71760.0
71447.0
71760.0
69771.0
70356.0

Now I want to delete those rows where in column 9 nothing
stands. My idea was the following one:

x=finalTable;
test='';
for i=1:(numberOfRows-1)
    a=x(i,9)
    if test==a
        for j=1:9
            x(i,j)=[];
        end
    end
end

Here an example: If we look in row 1 a=01_01_01_01 so
nothing is to do. It is the same with row 2. If we now look
at line 3 a=''. Now the if-cas eis true and all entries in
x in row 3 should be set to [] which means that the row
will be deleted.

At the moment I receive the error message: ??? Undefined
function or method 'eq' for input arguments of type 'cell'.

All in all I want to downsize x in the way that the rows
where column 9 is empty will be deleted.

Can anyone help me?

Thanks, Corinna

Subject: Re: comparison of cell entries

From: Titus

Date: 08 Aug, 2007 12:00:42

Message: 2 of 4

Hi Corinna,

several comments: line 4 should be a=x{i,9}, to get the element (string),
because, as the error message tells you, a is a cell array again, if you use
().
Next: to compare strings, use strcmp instead of ==, and instead of test=''
use isempty. So we end up with

x=finalTable;
for i=1:(numberOfRows-1)
   a=x{i,9};
   if isempty(a)
       for j=1:9
           x{i,j}=[];
       end
   end
end

Now: instead of marking them, just fill an array with true and false to kill
those rows later:

x=finalTable;
toBeDeleted = false(numberOfRows,1);
for i=1:(numberOfRows-1)
   a=x{i,9};
   if isempty(a)
    toBeDeleted(i) = true;
   end
end

Why not vectorize? You can do this in one step using cellfun
toBeDeleted = [cellfun(@isempty, x(1:numberOfRows-1,9)); false];

The false at the end, because you don't want to remove the last line?

and now delete the rows:
x(toBeDeleted, :) = [];

Titus




"Corinna Schmitt" <csc@mathworks.com> schrieb im Newsbeitrag
news:f9cabu$k9r$1@fred.mathworks.com...
> Hallo,
>
> I have a cell construction with 9 columns where the
> following data is stored:
>
> 1 1 1 1 1 AR_WIZ
> 165855.0 59748.0 01_01_01_01
> 1 1 1 1 0 AR_WIZ
> 139876.0 60372.0 01_01_01_01
> 71662.0
> 71760.0
> 71447.0
> 71760.0
> 69771.0
> 70356.0
>
> Now I want to delete those rows where in column 9 nothing
> stands. My idea was the following one:
>
> x=finalTable;
> test='';
> for i=1:(numberOfRows-1)
> a=x(i,9)
> if test==a
> for j=1:9
> x(i,j)=[];
> end
> end
> end
>
> Here an example: If we look in row 1 a=01_01_01_01 so
> nothing is to do. It is the same with row 2. If we now look
> at line 3 a=''. Now the if-cas eis true and all entries in
> x in row 3 should be set to [] which means that the row
> will be deleted.
>
> At the moment I receive the error message: ??? Undefined
> function or method 'eq' for input arguments of type 'cell'.
>
> All in all I want to downsize x in the way that the rows
> where column 9 is empty will be deleted.
>
> Can anyone help me?
>
> Thanks, Corinna


Subject: Re: comparison of cell entries

From: Corinna Schmitt

Date: 08 Aug, 2007 12:10:53

Message: 3 of 4

Hallo,

> Now I want to delete those rows where in column 9 nothing
> stands. My idea was the following one:
>
> x=finalTable;
> test='';
> for i=1:(numberOfRows-1)
> a=x(i,9)
> if test==a
> for j=1:9
> x(i,j)=[];
> end
> end
> end
>

I just solved the first error with the following code:

x=finalTable;
test='';
for i=1:(numberOfRows-1)
    a=x(i,9);
    b=i;
    if strcmp(test,a)==1
        for j=1:9
            x{i,j}=[];
        end
    end
end

But when I now check the size of x with 'whos' is the same
as before. The wanted rows are not deleted they are just
empty. I now constructed a new variable y:

y=cell(1576,9)
for counter=1:1576
    y(counter,1)=x(counter,1);
    y(counter,2)=x(counter,2);
    y(counter,3)=x(counter,3);
    y(counter,4)=x(counter,4);
    y(counter,5)=x(counter,5);
    y(counter,6)=x(counter,6);
    y(counter,7)=x(counter,7);
    y(counter,8)=x(counter,8);
    y(counter,9)=x(counter,9);
end

y has now the size I wanted.

But is there no easier way for this step?

Thanks, Corinna

Subject: Re: comparison of cell entries

From: us

Date: 08 Aug, 2007 14:05:21

Message: 4 of 4

Corinna Schmitt:
<SNIP wants to remove the little guys...

> Now I want to delete those rows where in column 9 nothing
stands...

one of the many solutions

% some data
     c{1,1}=1:10;
     c{2,1}=1:5;
     c{3,1}=1:15;
     c{4,1}=1:20;
% the engine
     len=cellfun(@length,c);
     c(len<10)=[];
% the result
     c

us

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
code us 08 Aug, 2007 10:10:22
cellfun us 08 Aug, 2007 10:10:22
length us 08 Aug, 2007 10:10:22
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics