MATLAB Answers

Error using tabular/addvars (line 174) Variables must have the same number of rows as the table they are being added to.

1 view (last 30 days)
BN
BN on 13 Feb 2020
Edited: BN on 14 Feb 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 Comments
BN
BN on 14 Feb 2020
Hello and thank you. Yes, I didn't think about that. Now I modified my A and B cells now are in 1 x 5. Also, I didn't know that it is impossible to assign the same name as the variable name for the table. So I want to use the capital words like DATE, LON, and LAT. I try to change them to the capital before merge tables using this code:
% Do you know how to merge this code? so with one for loop I be able to change all variable names?
for i = 1:numel(B)
B{i}.Properties.VariableNames = strrep(B{1}.Properties.VariableNames,'date','DATE');
end
for i = 1:numel(B)
B{i}.Properties.VariableNames = strrep(B{1}.Properties.VariableNames,'lon','LON');
end
for i = 1:numel(B)
B{i}.Properties.VariableNames = strrep(B{1}.Properties.VariableNames,'lat','LAT');
end
And after that I use this again:
% 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 still i get this error:
Error using tabular/addvars (line 174)
Variables must have the same number of rows as the table they are
being added to.
Here is new A and B.
I think the problem will fixed if I can delete after 336 rows in tables of A, But I don't know how to to that.

Sign in to comment.

Accepted Answer

JESUS DAVID ARIZA ROYETH
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

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by