Update multiple columns in table without loop, dot vs curly braces

2 ビュー (過去 30 日間)
Jan Kappen
Jan Kappen 2018 年 2 月 9 日
コメント済み: Jan Kappen 2018 年 2 月 12 日
Hi all,
I'd like to convert multiple columns (Variables) in a table object to another class like categorical or double. So far I have to use a loop, could this be simplified:
t = table(string(rand(3,1)),{'a';'b';'c'},{'aa';'bb';'cc'},...
'VariableNames',{'shallBeDouble', 'shallBeCategorical1', 'shallBeCategorical2'});
tBak = t;
catVars = {'shallBeCategorical1','shallBeCategorical2'};
for field = each(catVars)
t.(field) = categorical(t.(field));
end
doubleVars = {'shallBeDouble'};
for field = each(doubleVars)
t.(field) = double(t.(field));
end
disp(tBak)
disp(t)
output:
shallBeDouble shallBeCategorical1 shallBeCategorical2
_____________ ___________________ ___________________
"0.85303" 'a' 'aa'
"0.62206" 'b' 'bb'
"0.35095" 'c' 'cc'
shallBeDouble shallBeCategorical1 shallBeCategorical2
_____________ ___________________ ___________________
0.85303 a aa
0.62206 b bb
0.35095 c cc
I could use varfun to create a table or a cell of categorical values, but I can't assign them simultaneously:
t = table(string(rand(3,1)),{'a';'b';'c'},{'aa';'bb';'cc'},...
'VariableNames',{'shallBeDouble', 'shallBeCategorical1', 'shallBeCategorical2'});
catVars = {'shallBeCategorical1','shallBeCategorical2'};
t(:,catVars) = varfun(@categorical, t(:,catVars))
results in
The following error occurred converting from categorical to cell:
Conversion to cell from categorical is not possible.
Can the content of a column only be changed using the dot operator? I thought this would be equal: t.{:,'var1'} and t.var1?
Thanks!
  1 件のコメント
Jan Kappen
Jan Kappen 2018 年 2 月 12 日
Shall I try to rephrase the question somehow?

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

回答 (0 件)

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by