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

7 visualizzazioni (ultimi 30 giorni)
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 Commenti
fred  ssemwogerere
fred ssemwogerere il 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.
BN
BN il 14 Feb 2020
Modificato: BN il 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.

Accedi per commentare.

Risposta accettata

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

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Tables in Help Center e File Exchange

Tag

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by