Azzera filtri
Azzera filtri

How to assign a column of CATEGORICAL array to TABLE

25 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

dpb
dpb il 6 Ago 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 Commenti
Peter Perkins
Peter Perkins il 10 Ago 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 il 10 Ago 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.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by