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

7 views (last 30 days)
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 Comment
Sole
Sole on 23 Jun 2020
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

Sign in to comment.

Accepted Answer

Mohammad Sami
Mohammad Sami on 24 Jun 2020
% 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 Comments
Sole
Sole on 24 Jun 2020
when running t1.column5varname(logic1) = t1.column4varname(logic1), I get a "conversion to double from cell is not possible" error. What does that mean?

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!