Got Questions? Get Answers.
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:
Deleting element in a string

Subject: Deleting element in a string

From: sushma sharma

Date: 16 Feb, 2008 17:25:03

Message: 1 of 8

hi,

i have a cell array of strings formatted as follows

NDL_OK_2_R515_Japan

how can i delete the first letter after the third underscore
(i.e. delete the 'R').

i want the output to be:

NDL_OK_2_515_Japan

any help would be appreciated!

sushma

Subject: Deleting element in a string

From: us

Date: 16 Feb, 2008 17:33:01

Message: 2 of 8

"sushma sharma":
<SNIP wants to remove unwanted <char>s...

one of the many solutions

     s='NDL_OK_2_R515_Japan';
     ix=strfind(s,'_');
     s(ix(3)+1)='';
     disp(s);

us

Subject: Deleting element in a string

From: sushma sharma

Date: 16 Feb, 2008 17:53:02

Message: 3 of 8

thank you for the reply. it doesn't seem to work on cell
arrays...

sushma

Subject: Deleting element in a string

From: us

Date: 16 Feb, 2008 18:23:01

Message: 4 of 8

"sushma sharma":
<SNIP points out that <us> didn't read his/her OP
correctly...

sorry...
now one of the solutions might look like this

     s={
          'NDL_OK_2_R515_Japan'
          'NDL_OK_2_R515_Japan'
          'NDL_OK_2_R515_Japan'
     };
     ix=strfind(s,'_');
     r=cellfun(@(a,b) a([1:b(3),b(3)+2:end]),...
               s,ix,'uni',false);
     disp(r);
%{
     'NDL_OK_2_515_Japan'
     'NDL_OK_2_515_Japan'
     'NDL_OK_2_515_Japan'
%}

us

Subject: Deleting element in a string

From: Doug Schwarz

Date: 17 Feb, 2008 01:46:57

Message: 5 of 8

In article <fp79m5$a24$1@fred.mathworks.com>,
 "us " <us@neurol.unizh.ch> wrote:

> "sushma sharma":
> <SNIP points out that <us> didn't read his/her OP
> correctly...
>
> sorry...
> now one of the solutions might look like this
>
> s={
> 'NDL_OK_2_R515_Japan'
> 'NDL_OK_2_R515_Japan'
> 'NDL_OK_2_R515_Japan'
> };
> ix=strfind(s,'_');
> r=cellfun(@(a,b) a([1:b(3),b(3)+2:end]),...
> s,ix,'uni',false);
> disp(r);
> %{
> 'NDL_OK_2_515_Japan'
> 'NDL_OK_2_515_Japan'
> 'NDL_OK_2_515_Japan'
> %}
>
> us

And another solution could be (using s from above):

  regexprep(s,'([^_]*_){3}.(.*)','$1$2')

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

Subject: Deleting element in a string

From: us

Date: 17 Feb, 2008 02:00:20

Message: 6 of 8

Doug Schwarz:
<SNIP down to a true masterpiece

> regexprep(s,'([^_]*_){3}.(.*)','$1$2')

simply GRAND! as had to be expected from <ds>, of course...
:-)

us

Subject: Deleting element in a string

From: Doug Schwarz

Date: 17 Feb, 2008 02:22:23

Message: 7 of 8

In article <fp84fk$rgp$1@fred.mathworks.com>,
 "us " <us@neurol.unizh.ch> wrote:

> Doug Schwarz:
> <SNIP down to a true masterpiece
>
> > regexprep(s,'([^_]*_){3}.(.*)','$1$2')
>
> simply GRAND! as had to be expected from <ds>, of course...
> :-)
>
> us

Urs, thanks for the ego boost. Watch out though, I may not always live
up to your expectations! :-)

Isn't it like 3:00 AM in Z├╝rich? Get some rest!

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

Subject: Deleting element in a string

From: us

Date: 17 Feb, 2008 02:41:02

Message: 8 of 8

Doug Schwarz:
<SNIP sleepless in seattle - or was it rochester, or
zurich...

> Isn't it like 3:00 AM in Z?rich? Get some rest!

well, it's 3:39 am - but then: one <schwarz> ought to watch
over the other <schwarz>...

best with a :-)
urs

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