单元格数组X的第2列为我提供了以下代码:
等等......' 00000127'
' 00010121'
' 00040486'
' 00003702'
' 00010077'
' 00000002'
' 00000050'
我只想拥有最后的数字(右边的数字),例如:
' 127'
' 10121'
' 40486'
' 3702'
' 10077'
' 2'
' 50'
我发现困难,因为我想要删除元素左侧的零值。因此,除非它们在两个数字之间或在其他数字的右边,否则应该出现零值。
我该怎么办?
答案 0 :(得分:3)
一种混乱的方法 -
X(:,2) = strtrim(cellstr(num2str(cellfun(@str2num,X(:,2)))))
答案 1 :(得分:3)
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'
答案 2 :(得分:1)
使用正则表达式:
X(:,2) = cellfun(@(s) regexp(s, '(?<=^0*)[^0]\d*', 'match'), X(:,2));