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

7 Ansichten (letzte 30 Tage)
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 Kommentar
Sole
Sole am 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

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Mohammad Sami
Mohammad Sami am 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 Kommentare
Sole
Sole am 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?
Mohammad Sami
Mohammad Sami am 25 Jun. 2020
Maybe initialise the column 5 as cell array first
t1.column5varname = cell(height(t1),1);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Tables finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by