Clear Filters
Clear Filters

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

1 view (last 30 days)
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

Accepted Answer

Joel Van Sickel
Joel Van Sickel on 19 Aug 2020
Edited: Joel Van Sickel on 19 Aug 2020
Hello Arnold,
try this with no loop:
toDelete = (simResultsDomestic.type == "Domestic");
simResultsDomestic(toDelete,:) = [];
Regards,
Joel

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!