Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Topic: Help using textscan or sscanf
Replies: 21   Last Post: Jan 23, 2013 4:02 PM

 Messages: [ Previous | Next ]
 Kevin Ellis Posts: 62 Registered: 11/29/10
Re: Help using textscan or sscanf
Posted: Jan 16, 2013 4:21 PM

dpb <none@non.net> wrote in message <kd6qtq\$nu3\$1@speranza.aioe.org>...
> On 1/16/2013 11:30 AM, Kevin Ellis wrote:
> > Hello,
> >
> > My problem is very simple, but I cannot find an efficient way to
> > accomplish it. Here is an excerpt of my array (which is one column of a
> > dataset):
> >
> > Subset001.AccountNumber(1:6,:)
> >
> > ans =
> > '154045001'
> > '926665001'
> > '615017'
> > '1976936151'
> > '801700'
> > '4506702001'
> >
> > I am trying to create a new account number in the form of a string. I
> > need to append '4412' to the the last 9 digits of the above account
> > numbers. So for the results is:
> >
> > '4412154045001'
> > '4412926665001' - Account Numbers have 9 digits so this is easy
> > '4412615017' - Only has 6 digits so take the whole account number
> > '4412976936151' - Has 10 digits so drop first and append to '4412'
> > etc.
> >
> > So my problem is difficult in that I need to use sscanf to find the last
> > 9 digits of the account number...

>
> Nope...
>

> >> s
> =strvcat('154045001','926665001','615017','1976936151','801700','4506702001');
> >> [repmat('4412',6,1) s(:,end-9:end)]
> ans =
> 4412154045001
> 4412926665001
> 4412615017
> 44121976936151
> 4412801700
> 44124506702001

> >>
>
> I'd suggest in the end it _might_ be better to "normalize" the account
> no's by prepending zeros for the short strings. Then they would sort,
> etc., more nearly normally (for at least one definition of "normal",
> anyway).
>
> --

dpb,

Thanks for attempting the solution. For those that may have the same problem, I found the following solution. It is ugly, but works. I am working with datasets so I first add another column to the dataset counting the length of each element in the account numbers column using:

Subset.Count = (cellfun('length',Subset.AccountNumber));

Then because I have a dataset, I can parse into two distinct datasets for when count is greater than 9 (account numbers with greater than 9 digits) and for when count is less than 9 (account numbers with less than 9 digits:

Subset9DigL = Subset(Subset.Count > 9,{'Type','AccountNumber',etc.});

Subset9DigH = Subset(Subset.Count <= 9,{'Type','AccountNumber',etc.});

Then just a couple lines using cellfun to create the new account number for each case:

Subset9DigL.DocNo = cellfun(@(x) strcat('4412',sscanf(x,'%s',1),'001'),...
Subset9DigL.AccoutNumber,'UniformOutput',false);

And

Expr = arrayfun(@(x) strcat('%*',num2str((x-9)),'c%9c'),Subset9DigH.Count,...
'UniformOutput',false);

Subset9DigH.DocNo = cellfun(@(x,y) strcat('4412',sscanf(x,y,1),'001'),...
Subset9DigH.AccoutNumber,Expr,'UniformOutput',false);

Then I just merge the two datasets once more:

Subset = cat(1,Subset9DigL,Subset9DigH);

And remove the Count column:

Subset.Count = [];

Again, a terrible way to do this, but if anyone can think of anything more innovative, that would be great.

Kevin

Date Subject Author
1/16/13 Kevin Ellis
1/16/13 dpb
1/16/13 Kevin Ellis
1/16/13 dpb
1/16/13 Kevin Ellis
1/16/13 dpb
1/17/13 dpb
1/17/13 dpb
1/19/13 dpb
1/19/13 dpb
1/19/13 dpb
1/20/13 Kevin Ellis
1/20/13 dpb
1/20/13 james bejon
1/20/13 Kevin Ellis
1/20/13 james bejon
1/21/13 james bejon
1/22/13 Kevin Ellis
1/22/13 james bejon
1/23/13 Doug Schwarz
1/23/13 james bejon