Remove rows from a table based on the amount of rows in another table

I have two tables, Table1 and Table2 and I'm using them to create a master table called Final.
Table2 is considerably larger than Table1 and I dont need the extra data it provides in those rows. I can get this to work by just looking at the amount of rows and manually entering this value as below:
% Count rows in Table1
rowsd=size(Table1, 1);
% Remove extra rows in Table2
Table2([35400:end], :)=[];
This will remove the rows I dont need and all is good and I can then write my Final table. However, Table1 size will be different each time I use this script I'm writing due to differing input files. I want it to count the number of rows and then use that answer to automate this process.
I've tried the following but just get an error. Can anybody help please? Thank you
% Count rows in Table1
rowsd=size(Table1, 1);
% Remove extra rows in Table2
Table2([rowsd:end], :)=[];

5 Comments

used random table data, but did not find any error
Age = [38;43;38];
Height = [71;69;64];
Weight = [176;163;131];
Table1 = table(Age,Height,Weight); % table 1
rowsd=size(Table1,1);
Height1 = [71;69;64;67;64];
Height2 = [71;69;64;67;64];
Height3 = [71;69;64;67;64];
Height4 = [71;69;64;67;64];
Height5 = [71;69;64;67;64];
Table2=table(Height1,Height2,Height3,Height4,Height5) % table 2
Table2 = 5×5 table
Height1 Height2 Height3 Height4 Height5 _______ _______ _______ _______ _______ 71 71 71 71 71 69 69 69 69 69 64 64 64 64 64 67 67 67 67 67 64 64 64 64 64
rowsd2=size(Table2,1);
Table2([rowsd:end], :)=[]
Table2 = 2×5 table
Height1 Height2 Height3 Height4 Height5 _______ _______ _______ _______ _______ 71 71 71 71 71 69 69 69 69 69
Thanks for the answer but I still get the following error
To assign to or create a variable in a table, the number of rows must match the height of the table.
I have just realised the number of columns is also different, would that matter? 18 in Table1 and 3 in Table2
I tried adding 1 to rowsd and it now works perfectly. So the new code looks like this:
% Count rows in table1
rowsd=size(table1,1)+1;
% Count rows in table2
rowsd2=size(table2,1);
% Remove extra rows in table2
table2([rowsd:end], :)=[];
I cant paste the exact data sorry however, I noticed when I didnt add the 1 Table2 became a 4040x3 but it should be a 4041x3. It now matches Table1 perfectly. Thanks for your help

Sign in to comment.

Answers (0)

Categories

Products

Release

R2021b

Asked:

on 28 Feb 2022

Commented:

on 28 Feb 2022

Community Treasure Hunt

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

Start Hunting!