How to convert string cell array to int and NaN in Matlab?

Viewed 290

I have a string cell array that contains a mix of numbers and None values. I want to convert the None into NaN and numbers into int.

x = {'23','3','None'}
new_x = {23,3,NaN}
1 Answers

You can try cellfun with str2double, e.g.,

>> cellfun(@str2double,x,"UniformOutput", false)
ans =
{
  [1,1] = 23
  [1,2] = 3
  [1,3] = NaN
}

or another option (thank @Luis Mendo)

>> num2cell(str2double(x))
ans =
{
  [1,1] = 23
  [1,2] = 3
  [1,3] = NaN
}
Related