MATLAB Answers

Remove elements from string array

11 views (last 30 days)
anon
anon on 26 Nov 2019
Answered: Guillaume on 26 Nov 2019
What is the simpest way to remove string elements from an array? e.g. arr = [1, 2, 3, "x", "y", 10] would turn into [1,2,3,10]

  4 Comments

Show 1 older comment
anon
anon on 26 Nov 2019
Yes that is what I had discovered. What do you suppose I do in order to remove the "x" and "y" from the string array?
Guillaume
Guillaume on 26 Nov 2019
What is the rule that dictates which elements should be removed from the string array?
anon
anon on 26 Nov 2019
Create a new array that contains only integers. So after removing all non-integers the array would need to be turned into a numeric array e.g. [1,2,3,10]

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 26 Nov 2019
One possible way:
numericarray = double(yourstringarray); %convert string array to numeric. Text that can't be converted to numeric will end up as NaN.
numericarray = numericarray(mod(numericarray, 1) == 0); %only keep numbers that are integers. Will also remove NaNs.

  0 Comments

Sign in to comment.

Sign in to answer this question.

Tags