Store only numbers from user input
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi guys
Is there a way to store only the numbers from lets say an input of the type S^2+67*s+98
0 Commenti
Risposta accettata
Walter Roberson
il 1 Dic 2011
numstrs = regexp(TheString, '-?\d+', 'match');
then convert each of the cell strings returned in numstrs to numeric form. The -? part is to recognize an optional negative sign.
If you want to be able to accept floating point numbers, or if you want to recognize fractions as being numbers, or if you want to recognize pi as being a number, then the task gets more difficult.
Più risposte (1)
Jan
il 1 Dic 2011
Another approach from the times of Matlab 5.3 without REGEXP:
S = 'S^2+67*s+98';
S((S < '0' | S > '9') & S ~= '.') = ' ';
N = sscanf(S, '%g');
3 Commenti
Vedere anche
Categorie
Scopri di più su Logical in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!