# NaN from str2double('1 2 3') on Matlab 2020a

9 views (last 30 days)
Dawid Strzelczyk on 16 Oct 2020
Commented: James Tursa on 16 Oct 2020
Hi
str2double('1 2 3') returns NaN on Matlab 2020a but works fine on Matlab 2018b & Matlab 2016b (returns: 1 2 3).
How to convert a string of numbers to numbers in the newest Matlab version?
Best,
Dawid

#### 1 Comment

Stephen Cobeldick on 16 Oct 2020
For all MATLAB versions the most efficient soluton is:
str = '1 2 3';
vec = sscanf(str,'%f')
Avoid str2num, it contains a hidden eval call...

Bruno Luong on 16 Oct 2020
Edited: Bruno Luong on 16 Oct 2020
"works fine on Matlab 2018b"
Are you sure?
>> version
ans =
'9.5.0.1298439 (R2018b) Update 7'
>> str2double('1 2 3')
ans =
NaN
>> str2num('1 2 3')
ans =
1 2 3
>>

Show 1 older comment
Steven Lord on 16 Oct 2020
That is not the behavior I see.
>> version
ans =
'9.5.0.1033004 (R2018b) Update 2'
>> str2double('1 2 3')
ans =
NaN
Are you certain you're using the str2double function included with MATLAB? Alternately it uses sscanf extensively; are you using the version of that function included with MATLAB?
which -all str2double
which -all sscanf
Bruno Luong on 16 Oct 2020
AFAIK str2double never works on string with multiple numbers.
James Tursa on 16 Oct 2020
One more data point, PCWIN:
>> version
ans =
'9.5.0.944444 (R2018b)'
>> str2double('1 2 3')
ans =
NaN
>> str2num('1 2 3')
ans =
1 2 3

R2020a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!