Deleting strings in a list
52 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Niels Gustav Nortvig Willesen
am 26 Feb. 2018
Kommentiert: Stephen23
am 26 Feb. 2018
So I have a list containing strings like this
test = {'3.jpg', '4.jpg', '5.jpg', '6.jpg'};
I want to delete the specific entry with the name '4.jpg' completely, not just be left with ' ' - which is what the function erase does erase(test{2}, '4.jpg'). And in my project I do not know where this specific element is located.
for i = 1:length(test)
if strcmp(test{i}, '4.jpg') == 1
""Do something that deletes element i""
end
end
Thanks in advance
1 Kommentar
Akzeptierte Antwort
Jan
am 26 Feb. 2018
test = {'3.jpg', '4.jpg', '5.jpg', '6.jpg'};
test(strcmp(test, '4.jpg')) = []
0 Kommentare
Weitere Antworten (1)
Geoff Hayes
am 26 Feb. 2018
Niels - you could try
test(strcmp(test,'4.jpg')) = []
We use strcmpi(test,'4.jpg') to determine which elements of test string match to 4.jpg. The result is a logical array of zeros and ones (where a one indicates a match). We then set all those matches to be empty which "removes" the element from the list. And so test is now
test =
'3.jpg' '5.jpg' '6.jpg'
0 Kommentare
Siehe auch
Kategorien
Mehr zu String Parsing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!