简体   繁体   English

从单元格数组的元素中删除值

[英]Deleting values from an element of a cell array

Column 2 of cell array X provides me with the following codes: 单元格X的第2列为我提供了以下代码:

'00000127' '00000127'

'00010121' '00010121'

'00040486' '00040486'

'00003702' '00003702'

'00010077' '00010077'

'00000002' '00000002'

'00000050' '00000050'

etc … 等等...

And I only want to have the last numbers (the numbers on the right), for instance like this: 我只想要最后一个数字(右边的数字),例如:

'127' '127'

'10121' '10121'

'40486' '40486'

'3702' '3702'

'10077' '10077'

'2' '2'

'50' '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: str2num应该自动执行此操作:

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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