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:
String manipulation

Subject: String manipulation

From: Tim

Date: 2 Apr, 2013 15:01:06

Message: 1 of 3

I have a cell array of strings that I need to extract substrings from based on the location of hyphens. the array is something like this:
str = {'*88-123-*-001*';
         '*88-124-*-001*';
         '*88-125-*-001*';
         '*88-126-*-001*'}

where the * can be any number of characters. I am trying to extract just the numbers. If the strings were the same length identification of the first hyphen and last hyphen could occur followed by conversion to char.

str2=char(str);
hyphs = cell2mat(strfind(str,'-'));
out = cellstr(strcat(str2(:,hyphs(1)-2:hyphs(:,1)+3),str2(:,hyphs(end)+1:hyphs(end)+3)));

However with different lengths the indices of the hypens will change from row to row and that wont work properly. I would think there is a way to do this without a forloop but I cant think of it. Any suggestions, Thnx

Subject: String manipulation

From: Barry Williams

Date: 2 Apr, 2013 19:20:06

Message: 2 of 3

"Tim" wrote in message <kjerri$58n$1@newscl01ah.mathworks.com>...
> I have a cell array of strings that I need to extract substrings from based on the location of hyphens. the array is something like this:
> str = {'*88-123-*-001*';
> '*88-124-*-001*';
> '*88-125-*-001*';
> '*88-126-*-001*'}
>
> where the * can be any number of characters. I am trying to extract just the numbers. If the strings were the same length identification of the first hyphen and last hyphen could occur followed by conversion to char.
>
> str2=char(str);
> hyphs = cell2mat(strfind(str,'-'));
> out = cellstr(strcat(str2(:,hyphs(1)-2:hyphs(:,1)+3),str2(:,hyphs(end)+1:hyphs(end)+3)));
>
> However with different lengths the indices of the hypens will change from row to row and that wont work properly. I would think there is a way to do this without a forloop but I cant think of it. Any suggestions, Thnx

How about strtok?
Barry

Subject: String manipulation

From: Steven_Lord

Date: 2 Apr, 2013 20:53:51

Message: 3 of 3



"Tim " <timsema10@gmail.com> wrote in message
news:kjerri$58n$1@newscl01ah.mathworks.com...
> I have a cell array of strings that I need to extract substrings from
> based on the location of hyphens. the array is something like this:
> str = {'*88-123-*-001*';
> '*88-124-*-001*';
> '*88-125-*-001*';
> '*88-126-*-001*'}
>
> where the * can be any number of characters. I am trying to extract just
> the numbers. If the strings were the same length identification of the
> first hyphen and last hyphen could occur followed by conversion to char.
> str2=char(str);
> hyphs = cell2mat(strfind(str,'-'));
> out =
> cellstr(strcat(str2(:,hyphs(1)-2:hyphs(:,1)+3),str2(:,hyphs(end)+1:hyphs(end)+3)));
>
> However with different lengths the indices of the hypens will change from
> row to row and that wont work properly. I would think there is a way to do
> this without a forloop but I cant think of it. Any suggestions, Thnx

This may be overkill for this simple example but may be necessary for the
real-world application of which this is a simplified variant. [There's
almost always a more complicated example lurking in the wings.] Generate a
regular expression that does what you want then use REGEXP to apply it to
your data.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

No tags are associated with 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