Why is str2num returning [] and str2double returning NaN?

10 views (last 30 days)
I am new to Matlab and am having a hard ime figuring out how to turn a string into a number.
I have a line of text data that shows up as a cell, and I was able to convert that into a string. Then I created a new by extracting a specific part of the previous string. The specific part of the original string that I extracted is a single number. I want to turn it into a number so that I am able to use it to do a conversion.
This is what I have in my script:
v = A.textdata(5,:)
str = char(v)
newStr1 = extractBetween(char(v),'Accel sens, ',',counts/g')
newStr2 = extractBetween(char(v),'Mag sens, ',',counts/mT')
X = str2double('newStr1')
Y = str2doule('newStr2')
This is what is returned when I test the code:
(Y is cut off but it returned NaN)

Accepted Answer

dpb
dpb on 6 May 2019
X = str2double('newStr1')
Y = str2doule('newStr2')
You're trying to turn the literal character arrays 'newStr1' and 'newStr2' into numbers which, while they have an embedded numeric character aren't recognizable as numbers.
You intended to use the variables instead--
X = str2double(newStr1)
Y = str2doule(newStr2)

More Answers (0)

Categories

Find more on Data Type Conversion 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!