フィルターのクリア

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

1 回表示 (過去 30 日間)
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

採用された回答

Joel Van Sickel
Joel Van Sickel 2020 年 8 月 19 日
編集済み: Joel Van Sickel 2020 年 8 月 19 日
Hello Arnold,
try this with no loop:
toDelete = (simResultsDomestic.type == "Domestic");
simResultsDomestic(toDelete,:) = [];
Regards,
Joel
  1 件のコメント
Arnold Kuersteiner
Arnold Kuersteiner 2020 年 8 月 19 日
Thank you, that worked! Just changed the == to ~= as I wanted to remove those without "Domestic".

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by