Error using tabular/addvars (line 174) Variables must have the same number of rows as the table they are being added to.
5 views (last 30 days)
Show older comments
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 Comments
fred ssemwogerere
on 14 Feb 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.
Accepted Answer
JESUS DAVID ARIZA ROYETH
on 14 Feb 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 Comments
JESUS DAVID ARIZA ROYETH
on 14 Feb 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)
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!