5 views (last 30 days)

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.

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

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)

Opportunities for recent engineering grads.

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

Start Hunting!
## 2 Comments

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/505396-error-using-tabular-addvars-line-174-variables-must-have-the-same-number-of-rows-as-the-table-they#comment_797248

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/505396-error-using-tabular-addvars-line-174-variables-must-have-the-same-number-of-rows-as-the-table-they#comment_797248

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/505396-error-using-tabular-addvars-line-174-variables-must-have-the-same-number-of-rows-as-the-table-they#comment_797272

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/505396-error-using-tabular-addvars-line-174-variables-must-have-the-same-number-of-rows-as-the-table-they#comment_797272

Sign in to comment.