How to ensure that MATLAB reads the first column as a column in a table?

21 views (last 30 days)
coefficient_tab = cell2table(cell(0,5), 'VariableNames', {'Name', 'Estimate', 'SE', 'tStat', 'pValue'});
bundle = ['AB'; 'CD'; 'EF'; 'GH'];
for i = 1:length(bundle)
name = bundle(i, :);
tab = table([value1(i); value2(i)],... % elements of first column
[NaN; NaN],... % elements of second column
[NaN; NaN],...
[NaN; NaN],...
'VariableNames',{'Estimate', 'SE', 'tStat', 'pValue'},...
'RowNames',{strcat('var1_', name), strcat('var2_', name)});
coefficient_table = [coefficient_table; tab];
end
Here's a snapshot of the output from "tab" when i = AB
Estimate SE tStat pValue
________ ___ _____ ______
var1_AB 1.0693 NaN NaN NaN
var2_AB 2.1268 NaN NaN NaN
The output of tab is a table with 4 columns. The columns have values on 'estimate', 'SE', 'tstat', 'pValue'. However, MATLAB is not reading the column with the variable names as a column. So, instead of having 5, there are 4 columns. How can I adjust "tab" to ensure that the output has 5 columns and the first column is named as "Name" as mentioned in "coefficient_tab"?
So, I would like the table to look like:
Name Estimate SE tStat pValue
_____ ________ ___ _____ ______
var1_AB 1.0693 NaN NaN NaN
var2_AB 2.1268 NaN NaN NaN
I tried adding "Name" in "tab", but that didn't work
tab = table([value1(i); value2(i)],... % elements of first column
[NaN; NaN],... % elements of second column
[NaN; NaN],...
[NaN; NaN],...
'VariableNames',{'Name','Estimate', 'SE', 'tStat', 'pValue'},...
'RowNames',{strcat('var1_', name), strcat('var2_', name)})

Accepted Answer

Cris LaPierre
Cris LaPierre on 30 Jul 2022
You are using 'RowNames', which are not considered a table column. You therefore either need to use rownames everywhere (original table is 0x4) or you need to create a 5th variable containing the names and not use rownames.
% Using RowNames
coefficient_table = cell2table(cell(0,4), 'VariableNames', {'Estimate', 'SE', 'tStat', 'pValue'});
bundle = ['AB'; 'CD'; 'EF'; 'GH'];
value1 = rand(1,4);
value2 = rand(1,4);
for i = 1:length(bundle)
name = bundle(i, :);
coefficient_table = [coefficient_table; [{value1(i); value2(i)},... % elements of first column
{NaN; NaN},... % elements of second column
{NaN; NaN},...
{NaN; NaN}]];
end
coefficient_table.Properties.RowNames = ["var1_";"var2_"] + string(bundle)'
coefficient_table = 8×4 table
Estimate SE tStat pValue ________ ___ _____ ______ var1_AB 0.058393 NaN NaN NaN var2_AB 0.71082 NaN NaN NaN var1_CD 0.39971 NaN NaN NaN var2_CD 0.65425 NaN NaN NaN var1_EF 0.9427 NaN NaN NaN var2_EF 0.066835 NaN NaN NaN var1_GH 0.14773 NaN NaN NaN var2_GH 0.73154 NaN NaN NaN
Now the same code but using a 5th variable instead
coefficient_table = cell2table(cell(0,5), 'VariableNames', {'Name','Estimate', 'SE', 'tStat', 'pValue'});
bundle = ['AB'; 'CD'; 'EF'; 'GH'];
value1 = rand(1,4);
value2 = rand(1,4);
for i = 1:length(bundle)
name = bundle(i, :);
coefficient_table = [coefficient_table; ...
[{"var1_"+ name;"var2_"+ name},...
{value1(i); value2(i)},... % elements of first column
{NaN; NaN},... % elements of second column
{NaN; NaN},...
{NaN; NaN}]];
end
coefficient_table
coefficient_table = 8×5 table
Name Estimate SE tStat pValue _________ ________ ___ _____ ______ "var1_AB" 0.48427 NaN NaN NaN "var2_AB" 0.94618 NaN NaN NaN "var1_CD" 0.3964 NaN NaN NaN "var2_CD" 0.8816 NaN NaN NaN "var1_EF" 0.41481 NaN NaN NaN "var2_EF" 0.46532 NaN NaN NaN "var1_GH" 0.43444 NaN NaN NaN "var2_GH" 0.23924 NaN NaN NaN

More Answers (2)

Walter Roberson
Walter Roberson on 30 Jul 2022
MATLAB does not put a title on RowNames. If you want a title for that column, you will need to treat it as any other variable.
By the way: if you take an existing table, and you assign
tab(end+1, :) = EXPRESSION
and EXPRESSION is a cell array with as many entries as there are columns, then MATLAB will append the content of the cell entries as a new row in the table. So you could
tab(end+1,:) = {strcat('var1_', name), value1(i), nan, nan, nan};
tab(end+1,:) = {strcat('var2_', name), value2(i), nan, nan, nan};

dpb
dpb on 30 Jul 2022
rownames are table metadata; they are NOT a variable which is why the column variable heading doesn't show up for them -- the table has only four columns of data, not five. The RowNames property of the table returns the values or you can use the following --
tab.(tab.Properties.DimensionNames{1})
See table documentation about RowNames for details.
If you want the names to be a variable, then you have to create the variable that contains them.
You can construct the above table in much more succinct fashion, however --
vnames={'Estimate', 'SE', 'tStat', 'pValue'};
rnames=["Var"+repmat(string([1;2]),numel(bundle)/2,1)+"_"+string(char(kron(bundle,ones(2,1))))];
data=[abs(randn(8,1)) nan(8,3)];
tT=array2table(data,'VariableNames',vnames,'RowNames',rnames);
produces
>> tT
tT =
8×4 table
Estimate SE tStat pValue
________ ___ _____ ______
Var1_AB 0.21916 NaN NaN NaN
Var2_AB 1.0458 NaN NaN NaN
Var1_CD 0.95098 NaN NaN NaN
Var2_CD 0.7948 NaN NaN NaN
Var1_EF 0.07143 NaN NaN NaN
Var2_EF 0.77369 NaN NaN NaN
Var1_GH 0.77416 NaN NaN NaN
Var2_GH 0.26565 NaN NaN NaN
>>
  4 Comments
dpb
dpb on 31 Jul 2022
Bound to be at least a part of it, anyways, yes...although with still only 60 lines (unless the multipler of number of records/type is also gone up to a higher number than2) I'd not expect it to be all that big of an effect yet.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!