简体   繁体   中英

Deleting values from an element of a cell array

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?

一种杂乱的方法-

X(:,2) = strtrim(cellstr(num2str(cellfun(@str2num,X(:,2)))))

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'

使用正则表达式:

X(:,2) = cellfun(@(s) regexp(s, '(?<=^0*)[^0]\d*', 'match'), X(:,2));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM