Remove elements from string array

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

The first issue with your question is that the array [1, 2, 3, "x", "y", 10] cannot exist. You can't mix numbers and strings in a numeric or string array (you could in a cell array but the notation is different). Matlab will automatically convert the numbers to strings in order to create your array:
>> arr = [1, 2, 3, "x", "y", 10]
arr =
1×6 string array
"1" "2" "3" "x" "y" "10"
anon
anon 2019년 11월 26일
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 2019년 11월 26일
What is the rule that dictates which elements should be removed from the string array?
anon
anon 2019년 11월 26일
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]

댓글을 달려면 로그인하십시오.

답변 (1개)

Guillaume
Guillaume 2019년 11월 26일

0 개 추천

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.

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

질문:

2019년 11월 26일

답변:

2019년 11월 26일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by