I need to take characters out of a string using isnan and str2double.

2 次查看(过去 30 天)
Basically I need to take out the numeric values out of a string using these functions. I keep trying but some of the characters still come out as numbers.
This is an example
a='281-890-8905';
o=length(a);
for k=1:o
x=isnan(a(k));
if x==0
y=str2double(a(k));
end
end

采纳的回答

Thorsten
Thorsten 2016-10-26
cellfun(@(x) sscanf(x, '%f'), regexp(a, '(\d+)', 'match'))
  3 个评论
Rafael Perales
Rafael Perales 2016-10-26
This worked I just took out the plus sign to make it a single vector.Thank you

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2016-10-26
编辑:Jan 2016-10-26
Faster and simpler:
a = '281-890-8905';
s = a(a >= '0' & a <= '9') - '0';
Or:
s = a(isstrprop(a, 'digit')) - '0';

类别

Help CenterFile Exchange 中查找有关 Data Type Conversion 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by