From: "Steven_Lord" <>
Newsgroups: comp.soft-sys.matlab
Subject: Re: String manipulation
Date: Tue, 2 Apr 2013 16:53:51 -0400
Organization: MathWorks
Lines: 35
Message-ID: <kjfgh0$gcs$>
References: <kjerri$58n$>
Mime-Version: 1.0
Content-Type: text/plain;
Content-Transfer-Encoding: 7bit
X-Trace: 1364936032 16796 (2 Apr 2013 20:53:52 GMT)
NNTP-Posting-Date: Tue, 2 Apr 2013 20:53:52 +0000 (UTC)
In-Reply-To: <kjerri$58n$>
X-Priority: 3
X-MSMail-Priority: Normal
Importance: Normal
X-Newsreader: Microsoft Windows Live Mail 14.0.8089.726
X-MimeOLE: Produced By Microsoft MimeOLE V14.0.8089.726
Xref: comp.soft-sys.matlab:792505

"Tim " <> wrote in message 
> 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
To contact Technical Support use the Contact Us link on