Remove elements from string array
Ältere Kommentare anzeigen
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 Kommentare
Guillaume
am 26 Nov. 2019
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
am 26 Nov. 2019
Guillaume
am 26 Nov. 2019
What is the rule that dictates which elements should be removed from the string array?
anon
am 26 Nov. 2019
Antworten (1)
Guillaume
am 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.
Kategorien
Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!