0

Column 2 of cell array X provides me with the following codes:

'00000127'

'00010121'

'00040486'

'00003702'

'00010077'

'00000002'

'00000050'

etc …

And I only want to have the last numbers (the numbers on the right), for instance like this:

'127'

'10121'

'40486'

'3702'

'10077'

'2'

'50'

I am finding difficulties, because I want to erase the zero values I have on the left side of the element. So, unless they are in between two numbers or on the right of other number, zero values should go out.

How can I do it?

Community
  • 1
  • 1
user3557054
  • 219
  • 2
  • 11

3 Answers3

3

One sort of messy approach -

X(:,2) = strtrim(cellstr(num2str(cellfun(@str2num,X(:,2)))))
Divakar
  • 218,885
  • 19
  • 262
  • 358
3

str2num should automatically do that:

newcell=cellfun(@(x) str2num(x), cell, 'UniformOutput',false);

newcell=

    [  127]
    [10121]
    [40486]
    [ 3702]
    [10077]
    [    2]
    [   50]

And if you need them to be strings:

newcell=cellfun(@(x) num2str(str2num(x)), cell, 'UniformOutput',false);

newcell=

   '127'
    '10121'
    '40486'
    '3702'
    '10077'
    '2'
    '50'
Cape Code
  • 3,584
  • 3
  • 24
  • 45
1

Use a regular expression:

X(:,2) = cellfun(@(s) regexp(s, '(?<=^0*)[^0]\d*', 'match'), X(:,2));
Luis Mendo
  • 110,752
  • 13
  • 76
  • 147