Need Help Appending data to a table in every iteration from a for loop .I want to add new data to New_Table every loop but not overwrite the existing data.
149 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
s
el 25 de Sept. de 2018
Editada: Peter Perkins
el 28 de Jul. de 2020
filename = 'Notes.xls';
sheet = 1;
T = readtable(filename);
App_Names= T(:,4);
Unique_Data = unique(App_Names);
N = height (Unique_Data);
M = height(T);
for j = 1: N
app_name = Unique_Data{j,1}
p = (strcmpi(app_name,T{:,:}))
[row,col] = find(p)
New_Table = [ T(row,1),T(row,2),T(row,3),T(row,4);]
end
0 comentarios
Respuesta aceptada
KSSV
el 25 de Sept. de 2018
T = table(rand(2,1),rand(2,1),'VariableNames',{'Age','Weight'},'RowNames',{'P1','P2'}) ;
for i = 3:10
Tnew = table(rand,rand,'VariableNames',{'Age','Weight'},'RowNames',{['P',num2str(i)]}) ;
T = [T ; Tnew]
end
Más respuestas (1)
Peter Perkins
el 1 de Oct. de 2018
I can't follow the original code at all, but in general, it's not a great idea, performance-wise, to groew a table in a loop. This is much faster:
c = cell(1,n)
for i = 1:n
c{i} = table(...);
end
t = vertcat(c{:});
3 comentarios
Julio Perez Olvera
el 21 de Mayo de 2020
What if the variable names in tables inside "c" are not the same?
Peter Perkins
el 28 de Jul. de 2020
Editada: Peter Perkins
el 28 de Jul. de 2020
If the names are not the same, then why are you vertically concatenating them?
In my code snippet anobe, they will be the same (and set to the defaults), but if you want to vertically combine tables that have different variable names, then you have to reconcile the names, either by making the names on existing variables the same in all tables, or by adding variables to your tables to give them all the union of the different names.
That assumes that you want to turn all your tables into one longer table. Maybe you want a cell array, with a table in each cell, kept separately.
Ver también
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!