Prateek Daniels
Prateek Daniels

Reputation: 483

Convert a table column to a matrix in Matlab

I have a Table (T) as shown below:

>> Name = {'John';'Mary';'Martha'};
Age = [10.8;15.11;20.22];
T=table(Name,Age);
>> T

T =

  3×2 table

      Name       Age 
    ________    _____

    'John'       10.8
    'Mary'      15.11
    'Martha'    20.22

Converting the Age column to a matrix works fine, if I use:

cell2mat(table2cell(T(:,2)))

But doesn't work if I use:

cell2mat(table2cell(T.Age))

and throws the following errror:

>> cell2mat(table2cell(T.Age))
Undefined function 'getVars' for input
arguments of type 'double'.

Error in table2cell (line 15)
t_vars = getVars(t);

Is there any way I could use the column name T.Age instead using T(:,2) while converting this column into a matrix ?

Upvotes: 1

Views: 2790

Answers (1)

hargasinski
hargasinski

Reputation: 851

From the Access Data in a Table doc on the MathWorks site

Parentheses allow you to select a subset of the data in a table and preserve the table container.

while

Dot indexing extracts data from one table variable. The result is an array of the same data type as extracted variable. You can follow the dot indexing with parentheses to specify a subset of rows to extract from a variable.

So, if you use T.Age to access the data, the result should already be a matrix, i.e. you don't need to do any conversions using table2cell or cell2mat.

Upvotes: 4

Related Questions