Error using tabular/addvars (line 174) Variables must have the same number of rows as the table they are being added to.
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
BN
el 13 de Feb. de 2020
Hi, I have two cells, which include tables. I want to create a new cell and merges one-by-one tables into it.
For example, the first table in A merges with the first table in B, second with second, and so on. I want to insert copied columns after existing columns in A tables.
So tables in A are nx12, and tables in B are nx5 I want merged tables in the new cell to be nx17. I tried this:
% A is 1 x 5 and B is 1 x 10
% I don't know how to optimize this code
Newcell = []
for i=1:size(A,1)
for j=1:size(A,2)
Newcell{i,j}=addvars(A{i,j},B{i,j}.model_name,B{i,j}.date,B{i,j}.lon,B{i,j}.lat,B{i,j}.precip,'Before',2,'NewVariableNames',{'model_name','date','lon','lat','precip'});
end
end
But the error is:
Error using tabular/addvars (line 174)
Variables must have the same number of rows as the table they are
being added to.
If my code is correct and this error accrued because of row numbers are different in A and B, Would you please tell me how I can eliminate excess rows in A? Because actually I want to eliminate after 336 rows in A too.
Thanks.
2 comentarios
fred ssemwogerere
el 14 de Feb. de 2020
There are some issues i see with your workspace variables; "A" and "B".
1. The two are of different size (1x5 and 1x10). How will you concatenate tables in non-existent indices?
2. There are also a number of duplicates in both tables; "date","lon","lat". I think the aim should change to adding only specific variables in "B" to "A" as long as the two cell arrays are of equal size.
Respuesta aceptada
JESUS DAVID ARIZA ROYETH
el 14 de Feb. de 2020
for i = 1:numel(B)
B{i}.Properties.VariableNames = upper(B{i}.Properties.VariableNames);%change all variable names
end
newcell=cellfun(@(x,y) horzcat(x,y),cellfun(@(x) x(1:336,:),A,'uni',false),B,'uni',false)%merging A and B
3 comentarios
JESUS DAVID ARIZA ROYETH
el 14 de Feb. de 2020
assuming same size in each table :
newcell=cellfun(@(x,y) horzcat(x,y),cellfun(@(x) x(1:size(B{1},1),:),A,'uni',false),B,'uni',false)
Más respuestas (0)
Ver también
Categorías
Más información sobre Logical 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!