Home > database >  convert char to double in matlab: str2double
convert char to double in matlab: str2double

Time:02-24

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?

CodePudding user response:

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.

CodePudding user response:

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)

  • Related