Filling out an empty column in an existing table based on conditions matching other columns

7 ビュー (過去 30 日間)
I have a table. If column 3 is "fast" AND column 4 contains any of these characters: boat, car, or airplane. then column 5 become those strings on column 4. If it meets just the "fast" in column 3, then column 5 becomes "fast". Otherwise it's none. Do I need an if else statement? Or how would I do this?
column 3
fast
fast
slow
medium
fast
column 4
mike boat day
pax airplane oscar
ship
car
train
column 5
boat
airplane
none
none
fast
  1 件のコメント
Sole
Sole 2020 年 6 月 23 日
I tried this and it didn't work. It still gives me "fast" even though the allnames contains a 1 as the logic for detecting "boat" in the columns
t1 = table();
str = table2cell(lut(:,3));
allnames = contains(str,"boat");
if allnames == 1
t1.secondnames(allnames,:) = "boat";
else
t1.secondnames(allnames,:) = "fast";
end

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

採用された回答

Mohammad Sami
Mohammad Sami 2020 年 6 月 24 日
% t1 = yourtable
logic1 = startsWith(t1.column3varname,'fast') & startsWith(t1.column4varname,{'boat' 'car' 'airplane'});
logic2 = startsWith(t1.column3varname,'fast') & ~startsWith(t1.column4varname,{'boat' 'car' 'airplane'});
t1.column5varname(logic1) = t1.column4varname(logic1);
t1.column5varname(logic2) = t1.column3varname(logic2);
t1.column5varname(~logic1 & ~logic2) = {'none'};
  2 件のコメント
Sole
Sole 2020 年 6 月 24 日
when running t1.column5varname(logic1) = t1.column4varname(logic1), I get a "conversion to double from cell is not possible" error. What does that mean?
Mohammad Sami
Mohammad Sami 2020 年 6 月 25 日
Maybe initialise the column 5 as cell array first
t1.column5varname = cell(height(t1),1);

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

その他の回答 (0 件)

カテゴリ

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

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by