Warning: The assignment added rows to the table, but did not assign values to all of the table's existing variables. Those variables will extended with rows containing default values.

81 views (last 30 days)
Why do I recieve this warning?

Accepted Answer

dpb
dpb on 20 Oct 2019
Apparently you had a table with N variables but only assigned values to a subset of those when adding rows.
Example:
>> x=rand(3,1);y=rand(3,1); % some dummy data
>> t=table(x,y) % turn into table
t =
3×2 table
x y
________ _______
0.021833 0.20422
0.3931 0.66228
0.25254 0.91474
>> t.y(4)=0.5; % add a y row, but forget about x
Warning: The assignment added rows to the table, but did not assign values to all of the
table's existing variables. Those variables have been extended with rows containing default
values.
> In tabular/subsasgnDot (line 457)
In tabular/subsasgn (line 67)
>> t % so what did it do???
t =
4×2 table
x y
________ _______
0.021833 0.20422
0.3931 0.66228
0.25254 0.91474
0 0.5
>>
As shown, it added the y value as asked, but filled in a zero (default value) for x.
Always add for every variable (even if want the default value) to avoid the warning. You could disable the warning if doing this deliberately, but I really wouldn't recommend that over explicit assignment.
  2 Comments
Sven Larsen
Sven Larsen on 7 Nov 2022
your answer does not provide solution to problem at all. This error is received even, if we do:
t.x(4)=0.5;
t.y(4)=0.5;
Basic Matlab vagueness and annoyance...
dpb
dpb on 7 Nov 2022
Edited: dpb on 8 Nov 2022
Your code above still only added "x" the first time so the warning (and it is a warning, not a fatal error) is still correct; at that point there was still missing the corresponding "y"
As the Answer says, you must add all element of the table in the same operation to avoid the warning -- or, of course, you can always turn off the warning selectively if you're knowing you're going to be doing this in a section of code.
x=rand(3,1);y=rand(3,1); % some dummy data
t=table(x,y); % turn into table
xynew=[0.5 pi];
t=[t;array2table(xynew,'variablenames',t.Properties.VariableNames)]
t = 4×2 table
x y _______ ________ 0.50943 0.47535 0.50019 0.73985 0.80566 0.044596 0.5 3.1416
and, Voila! no warning.
I made an enhancement request/suggestion to create a new functionality for the table/timetable class of addrecord that would add the default record of proper type to the existing table either as empty, partially, or fully populated. Then, there would be specific syntax for the above.
t(end+1,:)=array2table(xynew,'variablenames',t.Properties.VariableNames)
t = 5×2 table
x y _______ ________ 0.50943 0.47535 0.50019 0.73985 0.80566 0.044596 0.5 3.1416 0.5 3.1416
would be an alternative syntax to do the same thing.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!