Borrar filtros
Borrar filtros

How to assign a column of CATEGORICAL array to TABLE

26 visualizaciones (últimos 30 días)
balandong
balandong el 5 de Ag. de 2017
Comentada: dpb el 10 de Ag. de 2017
Dear all, My understanding is TABLE each variable in the table to have a different data type. However, I cannot find a straight forward FUNCTION to import a categorical array into the Table.
For example, I want to export the categorical array name ConfMat into the column five of the Table T.
Thus I expect it can be done simply by
T(:,5) = ConfMat;
However, i got the following error
Right hand side of an assignment into a table must be another table or a cell array.
May I know how to solve this problem. Is it neccessary to convert the CATEGORICAL array into Cell array first?
The ConfMat and T are attached together in the MAT file together with this thread.
Thanks in advance for the time and wisdom

Respuesta aceptada

dpb
dpb el 6 de Ag. de 2017
Note what the error says -- can't augment a table by colon addressing except by another table or cell array. So, make a table and concatenate it...
>> T=[T table(ConfMat)];
>> T.Properties.VariableNames
ans =
'EasyComp1' 'EasyComp2' 'EasyComp3' 'EasyComp4' 'ConfMat'
>> t=T.ConfMat;
>> whos t
Name Size Bytes Class Attributes
t 120x1 434 categorical
>>
If you start with ConfMat as a numerical or cell string array, then another syntax that works is to go ahead and create the table with that variable as its initial class and then convert to categorical in place...
T.ConfMat=categorical(T.ConfMat);
It's not clear to me precisely why the restriction on the first syntax is required/couldn't be resolved at the interpreter level, but it is simpler coding certainly for the parser as is.
  4 comentarios
Peter Perkins
Peter Perkins el 10 de Ag. de 2017
In MATLAB, parenthesis subscripting preserves type. So if t is a table, t(...) returns a table, and t(...) = ... expects a table. It turns out that for assignment, t(...) = ... will let you get away with assigning from a cell array, as if you had called cell2table. It's a convenience mostly for the case of assigning one row. dpb, you might be asking, why not extend that convenience to any RHS? And the answer is, too much ambiguity.
In contrast, dot and braces on a table are about contents. Those are the syntaxes to use. So dpb's suggestion of dot subscripted assignment is in the right direction, and if you've used something like readtable and ConfMat is already in the table, then
T.ConfMat = categorical(T.ConfMat);
is the way to go. But if you have ConfMat as a text or numeric variable in your workspace, convert and assign directly:
T.ConfMat = categorical(ConfMat);
Subscripting behavior for tables is described in the doc.
dpb
dpb el 10 de Ag. de 2017
Yes, Peter, that's basically what I was asking and (I thought) had implied I knew the answer... :)
I'm still learning nuances of tables...and I could test but since here I guess the latter syntax does work to add the variable doesn't it? I got myself distracted by the initial posting's use of referencing to catenate more than the generic answer.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Resizing and Reshaping Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by