netbug
netbug

Reputation: 47

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

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}

Upvotes: 2

Views: 478

Answers (1)

ThomasIsCoding
ThomasIsCoding

Reputation: 101335

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
}

Upvotes: 4

Related Questions