MATLAB: Replace specific values in one column with value from the same row of another column

replacetablevalue

I have a table with 2 columns:
Column 1 containing categorical elements [A A B C Other C B Other] and
Column 2 with string elements ["NaN" "" "" "NaN" "D" "NaN" "NaN" "E"].
My goal is to get a string array with elements from column 1 where each Other is replaced with the respective value from column 2 (from the same row),
i.e. ["A" "A" "B" "C" "D" "C" "B" "E"]
Is there a simple way to do this?

Best Answer

Pretty simple, yes...first rebuild the table as described...
s=["NaN" "" "" "NaN" "D" "NaN" "NaN" "E"].';
c=categorical(cellstr({'A', 'A', 'B', 'C', 'Other', 'C', 'B', 'Other'})).';
t=table(c,s);
ix=t.c=='Other'; % find the wanted rows
t.c(ix)=categorical(t.s(ix)); % fill in the column
Convert to string class..
>> string(t.c)
ans =
8×1 string array
"A"
"A"
"B"
"C"
"D"
"C"
"B"
"E"
>>
Or, can convert the table in place if desired or bring outside the table if don't want to alter original or add a new variable column or whatever, depending the precise end result desired.