Find only numeric strings on cellstr array.
9 次查看(过去 30 天)
显示 更早的评论
I'd like to know the index of the "numerical strings" within a cellstr array.
arr = {'123', 'Hola', 'my', 'String', '', '1453', 'stay'};
A correct output will give me index 1 and 6.
isnumeric contained within cellfun doesn't work well here, I'd have to use it in combination with something else but I don't know exactly what that might be.
Any help is appreciated.
0 个评论
采纳的回答
Krishna Sutar
2021-6-15
From my understanding you want to find the indexes of the strings in the cell string array that are numerical. Here is the code which might help you:
arr = {'123', 'Hola', 'my', 'String', '', '1453', 'stay'};
idx = find(~isnan(str2double(arr)))
3 个评论
更多回答(2 个)
Walter Roberson
2021-6-15
arr = {'123', 'Hola', 'my', 'String', '', '1453', 'stay'};
find(~cellfun(@isempty, regexp(arr, '^\d+$', 'once')))
find(matches(arr, lineBoundary + digitsPattern + lineBoundary))
0 个评论
Stephen23
2021-6-15
编辑:Stephen23
2021-6-15
Writing regular expressions or pattern matching that robustly detects all valid number formats is not such a trivial task... it is more reliable to let MATLAB do the heavy lifting:
arr = {'123', 'Hola', 'my', 'String', '', '1453', 'stay', '-1.234', 'NaN', '9e-87'};
idx = strcmpi(arr,'NaN')|~isnan(str2double(arr))
find(idx)
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!