Equivalent of deal for a table

3 次查看(过去 30 天)
Kelly Kearney
Kelly Kearney 2015-7-28
回答: Bernard 2021-2-7
I often use deal to change the values of several cells in a cell array of strings:
a = 1:5;
b = {'one', 'two', 'three', 'four', 'five'};
[b{a>3}] = deal('big');
However, I just discovered that attempting the same operation on a table variable throws an error:
a = 1:5;
b = {'one', 'two', 'three', 'four', 'five'};
t = table(a,b);
[t.b{t.a>3}] = deal('big');
yields:
Expected one output from a curly brace or dot indexing expression, but there were 2
results.
Is there an equivalent syntax to quickly reassign values in a table?
  3 个评论
Kelly Kearney
Kelly Kearney 2015-7-28
Yes, looking further into it, it seems this is documented (though somewhat difficult to find) in the help for the @table/subsasgn method:
% LIMITATIONS:
%
% You cannot assign multiple values into a table variable or property using
% assignments such as
% [A.CellVar{1:2}] = deal(B1,B2),
% [A.StructVar(1:2).field] = deal(B1,B2), or
% [A.Properties.ObsNames{1:2}] = deal(B1,B2)
% Use multiple assignments of the form A.CellVar{1} = B1 instead.
Oh well.
Cedric
Cedric 2015-7-29
Hmm, not as bad as a rainbow color map ;-) but still.. well, hopefully this will become a built-in in a near future! (not 2015b though, because I have the same issue on the pre-release)

请先登录,再进行评论。

回答(1 个)

Bernard
Bernard 2021-2-7
It would be nice to have this functionality. In the meantime, this works, although not as elegant.
t.b(t.a>3) = repmat({'big'}, size(t.b(t.a>3)))

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by