How can I assign a single column of a 0x10 empty table

8 views (last 30 days)
Hello folks,
I have a 0x10 empty table. That is: the table variables are declared but there is no actual data entries in the table. Let's call it
myTable
I am trying to assign the first column of the table like so:
myTable.firstVariable = [0:1:length(x)]';
But alas this returns the following error:
To assign to or create a variable in a table, the number of rows must match the height of the table.
Of course, the height of the table is currently 0. Its height will be dictated by another variable x. I do not care what the other variables will be populated with at this time (i.e. [], or 0, or NaN, or whatever), I will populate them later.
How can I assign the first variable?
Thank you!
PS: I cannot use a struct or array. The datatype must remain as a table.

Accepted Answer

Star Strider
Star Strider on 29 Nov 2021
Preallocation for table arrays is possible.
myTable = table('Size',[10 1], 'VariableNames',{'firstVariable'}, 'VariableTypes',{'double'})
myTable = 10×1 table
firstVariable _____________ 0 0 0 0 0 0 0 0 0 0
myTable.firstVariable = (1:numel(myTable.firstVariable)).'
myTable = 10×1 table
firstVariable _____________ 1 2 3 4 5 6 7 8 9 10
See Preallocate Table and Fill Rows for an extended discussion.
.

More Answers (1)

Matt J
Matt J on 29 Nov 2021
Edited: Matt J on 29 Nov 2021
I cannot use a struct or array. The datatype must remain as a table.
You cannot avoid wokring with arrays as an intermediary, however, the data does not have to remain as an array. You can convert it to a table, e.g.,
T=nan(5,10);
T(:,1)=1:5;
T=array2table(T)
T = 5×10 table
T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 __ ___ ___ ___ ___ ___ ___ ___ ___ ___ 1 NaN NaN NaN NaN NaN NaN NaN NaN NaN 2 NaN NaN NaN NaN NaN NaN NaN NaN NaN 3 NaN NaN NaN NaN NaN NaN NaN NaN NaN 4 NaN NaN NaN NaN NaN NaN NaN NaN NaN 5 NaN NaN NaN NaN NaN NaN NaN NaN NaN

Categories

Find more on Tables in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!