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

1 vue (au cours des 30 derniers jours)
Rafael Perales
Rafael Perales le 26 Oct 2016
Commenté : Thorsten le 26 Oct 2016
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

Réponse acceptée

Thorsten
Thorsten le 26 Oct 2016
cellfun(@(x) sscanf(x, '%f'), regexp(a, '(\d+)', 'match'))
  3 commentaires
Rafael Perales
Rafael Perales le 26 Oct 2016
This worked I just took out the plus sign to make it a single vector.Thank you
Thorsten
Thorsten le 26 Oct 2016
Thank you Guillaume for pointing this out.

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 26 Oct 2016
Modifié(e) : Jan le 26 Oct 2016
Faster and simpler:
a = '281-890-8905';
s = a(a >= '0' & a <= '9') - '0';
Or:
s = a(isstrprop(a, 'digit')) - '0';

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by