Borrar filtros
Borrar filtros

Adding a New Column to a Table but get a warning?

10 visualizaciones (últimos 30 días)
wesleynotwise
wesleynotwise el 2 de Ag. de 2017
Comentada: wesleynotwise el 3 de Ag. de 2017
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

Respuesta aceptada

Steven Lord
Steven Lord el 2 de Ag. de 2017
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
  5 comentarios
Steven Lord
Steven Lord el 2 de Ag. de 2017
No, i is not the number of rows of Table2.NewStd, not all the time.
for i = 1:10
disp(i)
end
In this example, i will take on values from the vector 1:10. For one iteration, it will be 2. For another iteration, it will be 7. For the first iteration, it is 1.
Here's an example that does not involve a table that will hopefully clarify things.
A = [1; 2; 3; 4; 5]
A(1, 2) = 42
What is the size of A after executing these two commands?
I never assigned anything to A(4, 2) but it has a value. What is that value? Why does that element have that value?
This behavior is the same as the behavior for table, except table issues a warning when it grows a variable like that.
wesleynotwise
wesleynotwise el 3 de Ag. de 2017
Thanks again for your reply. I think I begin to understand what you tried to explain to me, but please let me me first sort few things out.
1. Sorry, my reply to i was wrong, it is a running number from 1 to the number of rows of Table2.NewStd (in my case, it is 1 to 500).
2. I don't understand this: " For one iteration, it will be 2. For another iteration, it will be 7. For the first iteration, it is 1."
3. I think the example is excellent. Although I still don't quite understand, I roughly know where the logic behind.
4. So your recommendation is to first fill Table2.NewStdCon with '0', and then replace by the output of my loop, right? Is there any other method?

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Tables en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by