Reputation: 1110
I would like to store multiple tables in one array. In my code below, I am creating two tables T1
and T2
. I want to store these tables into one variable MyArray
.
LastName = {'Sanchez';'Johnson';'Li';'Diaz';'Brown'};
Age = [38;43;38;40;49];
Smoker = logical([1;0;1;0;1]);
Height = [71;69;64;67;64];
Weight = [176;163;131;133;119];
BloodPressure = [124 93; 109 77; 125 83; 117 75; 122 80];
T1 = table(LastName,Age,Smoker);
T2 = table(Height,Weight,BloodPressure);
% The code below does not work
MyArray(1) = T1;
MyArray(2) = T2;
I know I can use a cell array but I would like to know if it is possible to create a table
datatype array in MATLAB.
Upvotes: 2
Views: 2527
Reputation: 25140
Because table
already implements ()
indexing, it's not really clear to me how you would expect to index MyArray
. Your example almost looks to me like MyArray = [T1, T2]
.
I'm not sure if it satisfies your needs, but you can have table
objects with table
variables, like this:
T = table(T1, T2);
You can then using indexing as normal, e.g.
T.T1.LastName{2}
Upvotes: 2
Reputation: 60444
There was a time when
builtin('subsref',T1,substruct('()',{1}))
(for any custom class T1
*) would skip calling the class-specific overloaded subsref
and use the built-in method instead. This would be equivalent to T1(1)
, but ignoring whatever the class defined for that syntax. Similarly for subsasgn
, which is the subscripted assignment operation T1(2)=T2
. This allowed the creation and use of arrays of a class.
However, this seems to no longer work. Maybe it is related to the classdef
-style classes, as the last time I used the trick above was before those were introduced.
I would suggest that you use cell arrays for this (even if the above still worked, I would not recommend it).
* Note that table
is a custom class, you can edit table
to see the source code.
Upvotes: 1