For loop for checking through categorical

5 ビュー (過去 30 日間)
Kacper Witasinski
Kacper Witasinski 2022 年 2 月 16 日
編集済み: Cris LaPierre 2022 年 2 月 16 日
MyTable = readtable("table.xlsx","TextType","string")
MyTable = convertvars(MyTable, ["Var 1" "Var 2"], "categorical")
result = [win, draw, lose]
for j = 1:length(MyTable)
if MyTable(j) == "Manchester City"
result = [win,0,0]
MyTable.Result(j) = result;
elseif MyTable(j) == "Chelsea"
result = [0,draw,0]
MyTable.Result(j) = result;
else
result = [0,0,lose]
MyTable.Result(j) = result;
end
end
Hello, I would like to create a for loop that checks through the categorical array of strings and if a string is "Manchester City" or "Chelsea" I would like to store the following values: win, draw, lose (stored in a string vector "result") in the new column. I'm keeping reciving some error:
()-indexing must appear last in an index expression.
I would appreciate any help. Thanks!

採用された回答

Cris LaPierre
Cris LaPierre 2022 年 2 月 16 日
編集済み: Cris LaPierre 2022 年 2 月 16 日
One observation is that your if statement conditional does not index a particular variable. Tell it which variable to look for "Manchester" in. If that data is in Var1, then your conditional should be
if MyTable.Var1(j) == "Manchester City"
elseif MyTable.Var1(j) == "Chelsea"
end
  1 件のコメント
Kacper Witasinski
Kacper Witasinski 2022 年 2 月 16 日
such a simple solution and somehow I could not find it... Thanks Cris.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by