Convert vector of characters to doubles/numeric
Show older comments
Hi,
I have a column of characters I would like to convert to doubles
'1.0118'
'1.0075'
'1.0064'
'1.0077'
'1.0087'
'1.0213'
'1.0213'
'1.013'
'1.013'
'1.0047'
'1.0039'
'0.9993'
'1.0059'
'1.0047'
'0.9985'
I have tried using str2double but it doesn't seem to work. How would I go about converting this into a column of doubles?
Thank you
Answers (2)
Ameer Hamza
on 29 Oct 2020
Is it available as a cell array. If yes, then try this
x = {
'1.0118'
'1.0075'
'1.0064'
'1.0077'
'1.0087'
'1.0213'
'1.0213'
'1.013'
'1.013'
'1.0047'
'1.0039'
'0.9993'
'1.0059'
'1.0047'
'0.9985'};
y = cellfun(@str2num, x)
8 Comments
James Higgins
on 29 Oct 2020
Ameer Hamza
on 30 Oct 2020
In MATLAB, character arrays cannot have a different number of characters in rows. For example
'1.0213'
'1.013'
'1.013'
'1.0047'
such a thing is not possible in a MATLAB array. Can you show in which form you have the characters? Maybe attach the variable in a .mat file.
James Higgins
on 30 Oct 2020
Ameer Hamza
on 30 Oct 2020
The screenshot is enough to give the idea. You can do something like this
numeric_values = str2double({S.Observations.value})
Walter Roberson
on 30 Oct 2020
str2double(value)
James Higgins
on 30 Oct 2020
Ameer Hamza
on 31 Oct 2020
You need to use braces instead of parenthesis
date = datetime({S.observations.date}, 'InputFormat','yyyy-MM-dd')
James Higgins
on 31 Oct 2020
Walter Roberson
on 30 Oct 2020
str2double(cellstr(YourCharacterArray))
Categories
Find more on Cell Arrays 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!