How to make str2double recognize comma delimited numbers?
Show older comments
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 Comments
Mathieu NOE
on 19 Mar 2025
can you share you excel file and a piece of working code ?
KD
on 19 Mar 2025
Accepted Answer
More Answers (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
on 19 Mar 2025
Edited: Mike Croucher
on 19 Mar 2025
Try using str2num instead
str2num('2,3',Evaluation = "restricted")
or possibly this
s = '2,3';
str2double(split(s, ','))
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!