'convert char to double in matlab: str2double

X= 1:63;
n = 6;
% Y = int2bit(X,n)
y=dec2bin(X, n)

with this example I tried str2double(y) and got NaN

What is a problem?



Solution 1:[1]

str2double will only convert text that represents real or complex scalar values. Where y is a char array of binary values. It is basically interpreting y as one large integer. Hence, it will return NaN or Inf depending on the version of MATLAB you are using.

Solution 2:[2]

You can use convertCharsToStrings and then use str2double

e.g

for i = 1:length(y)
  tempvar = convertCharsToStrings(y(i,:));
  x1(i) = str2double(tempvar);
end

OR if you just want to convert all string into double then use

arrayfun(@(x)str2double(convertCharsToStrings(x)),y,'Uniformoutput',false)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 James Crawford
Solution 2