MATLAB: Adding a New Column to a Table but get a warning

adding columnif else statementtable

Hello. I wanted to add a new column from a (500 x 4) Table, with some conditions, to another (500 x 13) Table. Not so sure why I get this warning sign, though they have the same number of row:
Warning: The new variables being added to the table have fewer rows than
the table. They have been extended with rows containing default values.
> In tabular/subsasgnDot (line 327)
In tabular/subsasgn (line 67)
Here is my code:
for i=1:size(Table1.Std,1); % count the number of rows in Table 1 (500x4)
if Table1.Std(i) > 10 || Table1.Std(i) <-10; % conditions applied
Table2.NewStd(i) =1; % adding a new column in Table 2
else Table2.NewStd(i) =0;
end
end

Best Answer

What is size(1, 1)? The scalar 1 has exactly 1 row. Since you receive that warning the table Table2 must have more than 1 row. As a simpler example:
>> T = table((1:3).', 'VariableNames', {'Variable1'});
>> numberOfRows = height(T)
numberOfRows =
3
>> T.Variable2(1) = 17
Warning: The new variables being added to the table have fewer rows than the table.
They have been extended with rows containing default values.
In this case I added a new variable with 1 row to a table with 3 rows. The new variable must contain the same number of rows as the existing variable(s) so MATLAB issues a warning and fills in the extra spaces. Since the new variable contains double precision data, the default value used to fill in is 0.
In this case, you can use logical indexing.
% Preallocate
T.Variable3 = zeros(height(T), 1);
% Update
T.Variable3(T.Variable2 > 0) = 42
or create the variable of the correct size first, then add the whole variable in one statement.
V4 = T.Variable1+T.Variable2-T.Variable3;
T.Variable4 = V4