How to make str2double recognize comma delimited numbers?
Afficher commentaires plus anciens
I'm reading in an excel file and I have a column that I need to convert all the values from string to text.
Currently Im using:
data.col1 = str2double(data.col1);
and this is merging the values in my column.
For example, it goes from '2,3' to 23. Is there a way I can do this, but make it so it recognizes it is 2 separate numbers 2 and 3?
Thanks!
2 commentaires
Mathieu NOE
le 19 Mar 2025
can you share you excel file and a piece of working code ?
KD
le 19 Mar 2025
Réponse acceptée
Plus de réponses (2)
Using SSCANF will likely be more efficient than relying on regular expressions:
T = readtable('ExampleExcel.xlsx')
F = @(t)sscanf(t,'%f,',[1,Inf]);
T.RC = cellfun(F, T.RC, 'uni',0)
Mike Croucher
le 19 Mar 2025
Modifié(e) : Mike Croucher
le 19 Mar 2025
Try using str2num instead
str2num('2,3',Evaluation = "restricted")
or possibly this
s = '2,3';
str2double(split(s, ','))
1 commentaire
KD
le 19 Mar 2025
Catégories
En savoir plus sur Spreadsheets dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!