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:
Logical index of strings in cell array that don't start with A-Z

Subject: Logical index of strings in cell array that don't start with A-Z

From: AJP

Date: 8 May, 2012 15:44:07

Message: 1 of 6

I have an Nx1 cell array of strings. How can I get a logical index array of the rows that do not begin with an upper-case letter character (A-Z)?

Surely there is a one-line solution using regexp()?

I'm new to regular expressions and the syntax is bewildering. Can someone assist? Many thanks.

Subject: Logical index of strings in cell array that don't start with

From: Nasser M. Abbasi

Date: 8 May, 2012 16:11:06

Message: 2 of 6

On 5/8/2012 10:44 AM, AJP wrote:
> I have an Nx1 cell array of strings. How can I get a logical index
>array of the rows that do not begin with an upper-case letter character (A-Z)?
>
> Surely there is a one-line solution using regexp()?
>
> I'm new to regular expressions and the syntax is bewildering.
>Can someone assist? Many thanks.

Ok, how about being a little nice and provide a small example
of this cell array with few example strings so that who ever
wants to help you do not have to make one up themselves?

5-6 cells will be enough.

--Nasser

Subject: Logical index of strings in cell array that don't start with

From: Doug Schwarz

Date: 8 May, 2012 16:30:33

Message: 3 of 6

On 5/8/2012 11:44 AM, AJP wrote:
> I have an Nx1 cell array of strings. How can I get a logical index array
> of the rows that do not begin with an upper-case letter character (A-Z)?
>
> Surely there is a one-line solution using regexp()?
>
> I'm new to regular expressions and the syntax is bewildering. Can
> someone assist? Many thanks.

cellfun(@isempty,regexp(strings,'^[A-Z]'))

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

Subject: Logical index of strings in cell array that don't start with

From: AJP

Date: 8 May, 2012 16:41:07

Message: 4 of 6

> Ok, how about being a little nice and provide a small example
> of this cell array with few example strings so that who ever
> wants to help you do not have to make one up themselves?
>
> 5-6 cells will be enough.
>
> --Nasser


Quite right, apologies Nasser. Here is an example of the kind of cell array I'm working with.

ID={'_string1';
'_string2';
'_string3';
'1string1';
'2string2';
'5string3';
'Astring1';
'Bstring1';
'Cstring1';
'Dstring1';
'Zstring1';}

I think the suggestion shown above using cellfun() and isempty() is going to work. Give me a minute to try it out.

Subject: Logical index of strings in cell array that don't start with

From: Nasser M. Abbasi

Date: 8 May, 2012 16:57:52

Message: 5 of 6

On 5/8/2012 11:41 AM, AJP wrote:
>
>Here is an example of the kind of cell array I'm working with.
>
> ID={'_string1';
> '_string2';
> '_string3';
> '1string1';
> '2string2';
> '5string3';
> 'Astring1';
> 'Bstring1';
> 'Cstring1';
> 'Dstring1';
> 'Zstring1';}
>

thanks. It always helps if someone provides tiny data
example of the problem they are asking about along
with the question. (when possible).

This helps make it easier to answer because one then do not
have to make up the data themselves in order to answer it.

The Matlab experts here can answer questions without
even seeing data example or using data, but the rest of
us need to make up data to verify our answer on before
replying.

--Nasser

Subject: Logical index of strings in cell array that don't start with

From: AJP

Date: 8 May, 2012 17:05:07

Message: 6 of 6

> cellfun(@isempty,regexp(strings,'^[A-Z]'))

Yes, that's it. Thanks, Doug.

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