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:
Find cells with alphabetic characters

Subject: Find cells with alphabetic characters

From: Nate

Date: 4 Oct, 2011 16:01:26

Message: 1 of 5

I have a cell array that looks like this:

data: 3x1 cell:
    'abc'
    '1'
    '2'

I want to find all of the cells with alphabetic characters in them.
Currently, I am doing this:

for i = 1:26
    regexpi(data,char(64+i))
end

Is there a faster, vectorized way to do this?

Thanks,
Nate

Subject: Find cells with alphabetic characters

From: Doug Schwarz

Date: 4 Oct, 2011 16:17:31

Message: 2 of 5

On 10/4/2011 12:01 PM, Nate wrote:
> I have a cell array that looks like this:
>
> data: 3x1 cell:
> 'abc'
> '1'
> '2'
>
> I want to find all of the cells with alphabetic characters in them.
> Currently, I am doing this:
>
> for i = 1:26
> regexpi(data,char(64+i))
> end
>
> Is there a faster, vectorized way to do this?
>
> Thanks,
> Nate

has_alpha = ~cellfun(@isempty,regexp(data,'[a-zA-Z]'));

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: Find cells with alphabetic characters

From: Nate

Date: 4 Oct, 2011 16:23:29

Message: 3 of 5

Doug Schwarz <see@sig.for.address.edu> wrote in message <vkGiq.7626$Ij7.3988@newsfe07.iad>...
> has_alpha = ~cellfun(@isempty,regexp(data,'[a-zA-Z]'));
>
> --
> Doug Schwarz
> dmschwarz&ieee,org
> Make obvious changes to get real email address.

Solid, thanks Doug. I saw something like that in the help, I just couldn't make any sense of it.

Subject: Find cells with alphabetic characters

From: Jos (10584)

Date: 6 Oct, 2011 15:16:30

Message: 4 of 5

Doug Schwarz <see@sig.for.address.edu> wrote in message <vkGiq.7626$Ij7.3988@newsfe07.iad>...
> On 10/4/2011 12:01 PM, Nate wrote:
> > I have a cell array that looks like this:
> >
> > data: 3x1 cell:
> > 'abc'
> > '1'
> > '2'
> >
> > I want to find all of the cells with alphabetic characters in them.
> > Currently, I am doing this:
> >
> > for i = 1:26
> > regexpi(data,char(64+i))
> > end
> >
> > Is there a faster, vectorized way to do this?
> >
> > Thanks,
> > Nate
>
> has_alpha = ~cellfun(@isempty,regexp(data,'[a-zA-Z]'));
>

You can avoid regexp

% data
  data = {'ok', 1 , '' , [] , 'ok2', '123'}
% engine
  fh = @(x) ischar(x) && ~isempty(x) && any(lower(x) >= 'a' & lower(x) <= 'z')
  TF = cellfun(fh, data)
  data(TF)

hth
~ Jos

Subject: Find cells with alphabetic characters

From: Nate

Date: 6 Oct, 2011 16:31:10

Message: 5 of 5

"Jos (10584)" wrote in message <j6kgoe$b6a$1@newscl01ah.mathworks.com>...
> You can avoid regexp
>
> % data
> data = {'ok', 1 , '' , [] , 'ok2', '123'}
> % engine
> fh = @(x) ischar(x) && ~isempty(x) && any(lower(x) >= 'a' & lower(x) <= 'z')
> TF = cellfun(fh, data)
> data(TF)
>
> hth
> ~ Jos

Excellent. Thank you.

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