How to ensure that MATLAB reads the first column as a column in a table?
40 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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)})
0 commentaires
Réponse acceptée
Cris LaPierre
le 30 Juil 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)'
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
Plus de réponses (2)
Walter Roberson
le 30 Juil 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};
0 commentaires
dpb
le 30 Juil 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})
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 commentaires
dpb
le 31 Juil 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.
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!