- important: . does NOT match the period character, but in fact it matches any character. To only match the period, you need to escape it: \.
- \d matches any digit, and is simpler than writing [0-9]
- by making the decimal fraction optional my answer will also match integer values
using regexp with negative and positive floating numbers
46 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
sermet
le 24 Mai 2017
Commenté : Alberto Fernandez Osorio
le 11 Mai 2022
data =
'0.00640869<'
'0.00640869<'
'-0.00598145'
'-0.00598145'
'-0.0010376<'
'-0.0010376<'
'-0.00604248'
'-0.00213623'
'0.00567627<'
'-0.00219727'
'0.00567627<'
% 11x1 cell
to remove the non-numeric character;
data_num = regexp(data,'[0-9]+.[0-9]+', 'match');
In this case, negative signs (-) are also removed. How can I modify this code to get - signs?
0 commentaires
Réponse acceptée
Stephen23
le 24 Mai 2017
Modifié(e) : Stephen23
le 24 Mai 2017
regexp(data,'[+-]?\d+\.?\d*', 'match');
Notes:
Read the MATLAB documentation for more information:
You might also like to try my FEX submission, which lets you experiment with regular expressions and see their output in real time (as you type):
5 commentaires
Stephen23
le 24 Mai 2017
Modifié(e) : Stephen23
le 24 Mai 2017
@Walter Roberson: the tests I just ran showed that your code is actually slightly slower (1e5 iterations):
Elapsed time is 3.67537 seconds. % \.?\d*
Elapsed time is 3.91039 seconds. % (\.\d*)?
I have no idea how TMW implements this, or whether it would be stable between versions...
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical 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!