Path: news.mathworks.com!not-for-mail
From: "Steven_Lord" <slord@mathworks.com>
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$1@newscl01ah.mathworks.com>
References: <kjerri$58n$1@newscl01ah.mathworks.com>
NNTP-Posting-Host: ah-slord.dhcp.mathworks.com
Mime-Version: 1.0
Content-Type: text/plain;
	format=flowed;
	charset="UTF-8";
	reply-type=response
Content-Transfer-Encoding: 7bit
X-Trace: newscl01ah.mathworks.com 1364936032 16796 172.28.9.169 (2 Apr 2013 20:53:52 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 2 Apr 2013 20:53:52 +0000 (UTC)
In-Reply-To: <kjerri$58n$1@newscl01ah.mathworks.com>
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: news.mathworks.com comp.soft-sys.matlab:792505



"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