How can I use table() with a variable number of columns?
I’m making a set of tables from a 4x4x4 array, like so:
T{jj} = table(data(jj,:,1)’,data(jj,:,2)’,data(jj,:,3)’,data(jj,:,4)’,’VariableNames’,colName,’RowNames’,rowName);
The annoying thing is, I have to change this line every time there’s a different number of columns. For example, for 3 columns, the command is:
T{jj} = table(data(jj,:,1)’,data(jj,:,2)’,data(jj,:,3)’,’VariableNames’,colName,’RowNames’,rowName);
I know I could do a switch on the number of columns and have a case for every possible number of columns I might ever have, but is some way around that? The obvious solution of giving it the data as an array doesn’t work if you want named rows and columns:
T{jj} = table(data(jj,:,:),’VariableNames’,colName,’RowNames’,rowName);
Error using table (line 369)
The VariableNames property must contain one name for each variable in the table.
I get the exact same error back if I use slice():
T{jj} = table(slice(data(jj,:,:)),’VariableNames’,colName,’RowNames’,rowName);
But it works fine without row / column names
T{jj} = table(data(jj,:,:));
Am I missing something simple, or is this maybe a design oversight?I’m making a set of tables from a 4x4x4 array, like so:
T{jj} = table(data(jj,:,1)’,data(jj,:,2)’,data(jj,:,3)’,data(jj,:,4)’,’VariableNames’,colName,’RowNames’,rowName);
The annoying thing is, I have to change this line every time there’s a different number of columns. For example, for 3 columns, the command is:
T{jj} = table(data(jj,:,1)’,data(jj,:,2)’,data(jj,:,3)’,’VariableNames’,colName,’RowNames’,rowName);
I know I could do a switch on the number of columns and have a case for every possible number of columns I might ever have, but is some way around that? The obvious solution of giving it the data as an array doesn’t work if you want named rows and columns:
T{jj} = table(data(jj,:,:),’VariableNames’,colName,’RowNames’,rowName);
Error using table (line 369)
The VariableNames property must contain one name for each variable in the table.
I get the exact same error back if I use slice():
T{jj} = table(slice(data(jj,:,:)),’VariableNames’,colName,’RowNames’,rowName);
But it works fine without row / column names
T{jj} = table(data(jj,:,:));
Am I missing something simple, or is this maybe a design oversight? I’m making a set of tables from a 4x4x4 array, like so:
T{jj} = table(data(jj,:,1)’,data(jj,:,2)’,data(jj,:,3)’,data(jj,:,4)’,’VariableNames’,colName,’RowNames’,rowName);
The annoying thing is, I have to change this line every time there’s a different number of columns. For example, for 3 columns, the command is:
T{jj} = table(data(jj,:,1)’,data(jj,:,2)’,data(jj,:,3)’,’VariableNames’,colName,’RowNames’,rowName);
I know I could do a switch on the number of columns and have a case for every possible number of columns I might ever have, but is some way around that? The obvious solution of giving it the data as an array doesn’t work if you want named rows and columns:
T{jj} = table(data(jj,:,:),’VariableNames’,colName,’RowNames’,rowName);
Error using table (line 369)
The VariableNames property must contain one name for each variable in the table.
I get the exact same error back if I use slice():
T{jj} = table(slice(data(jj,:,:)),’VariableNames’,colName,’RowNames’,rowName);
But it works fine without row / column names
T{jj} = table(data(jj,:,:));
Am I missing something simple, or is this maybe a design oversight? table MATLAB Answers — New Questions