2

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 Answer 1

4

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
}
Sign up to request clarification or add additional context in comments.

1 Comment

@ThomasIsCoding Ah, I hadn't noticed that the result has to be a cell array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.