[英]Deleting values from an element of a cell array
单元格X的第2列为我提供了以下代码:
'00000127'
'00010121'
'00040486'
'00003702'
'00010077'
'00000002'
'00000050'
等等...
我只想要最后一个数字(右边的数字),例如:
'127'
'10121'
'40486'
'3702'
'10077'
'2'
'50'
我遇到了困难,因为我想擦除元素左侧的零值。 因此,除非它们在两个数字之间或在另一个数字的右边,否则零值应消失。
我该怎么做?
一种杂乱的方法-
X(:,2) = strtrim(cellstr(num2str(cellfun(@str2num,X(:,2)))))
str2num
应该自动执行此操作:
newcell=cellfun(@(x) str2num(x), cell, 'UniformOutput',false);
newcell=
[ 127]
[10121]
[40486]
[ 3702]
[10077]
[ 2]
[ 50]
如果您需要将它们作为字符串:
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.