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 CenterFile Exchange에서 Tables에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by