Effacer les filtres
Effacer les filtres

"Index in position 1 exceeds array bounds" for deleting rows if containing string

1 vue (au cours des 30 derniers jours)
Hi everyone,
I am attempting to delete rows from the 500x5 table simResultsDomestic if they do not contain the string "Domestic", as below:
typeSample startSample durationSample endSample energy
__________ ___________ ______________ _________ _______
"Domestic" 999 690 249 11.413
"Domestic" 1242 970 772 16.044
"Domestic" 664 660 1324 10.916
"Fast" 932 1050 542 29.615
"Domestic" 967 590 117 9.7586
I have attempted two methods for this, but they both give me the same error message: "Index in position 1 exceeds array bounds (must not exceed 400)." 400 because for some reason my code manages to remove the correct rows (so the table reduces in size from 500 to 400 rows), yet then gives me the error message and stops running.
Method 1:
for z=1:height(simResultsDomestic)
if ~strcmp(simResultsDomestic.typeSample(z), "Domestic")
simResultsDomestic(z,:)=[];
end
end
Method 2:
for z=1:height(simResultsDomestic)
if simResultsDomestic.typeSample(z,:) ~= "Domestic"
simResultsDomestic(z,:)=[];
end
end
I feel like this might be an obvious one but I'm not able to find the solution.
Many thanks for any help,
Arnold

Réponse acceptée

Joel Van Sickel
Joel Van Sickel le 19 Août 2020
Modifié(e) : Joel Van Sickel le 19 Août 2020
Hello Arnold,
try this with no loop:
toDelete = (simResultsDomestic.type == "Domestic");
simResultsDomestic(toDelete,:) = [];
Regards,
Joel
  1 commentaire
Arnold Kuersteiner
Arnold Kuersteiner le 19 Août 2020
Thank you, that worked! Just changed the == to ~= as I wanted to remove those without "Domestic".

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by