trying to pad smaller table to larger table

10 views (last 30 days)
i have a csv file that table thats a (20 x 30,0000) and i calculated data thats (1 x 27) and need to add that to that csv file. im stuck on what to do and cant seem to figure this out.
code is:
im sure im doing something wrong but have no clue what it could be.
Table1 = [Speed_50_1, Speed_50_2, Speed_55_1, Speed_55_2, Speed_60_1, Speed_60_2, Speed_65_1, Speed_65_2, Speed_70];
PD = padarray(Table1,[1,0],0)
T_preserve = readtable('Tesla_Steady_State_027.csv','Delimiter',',');
T_new = [T_preserve, PD];
writetable(T_new, 'Tesla_Steady_State_027.csv');
  1 Comment
Walter Roberson
Walter Roberson on 2 Nov 2020
i have a csv file that table thats a (20 x 30,0000)
Could you confirm that you have a table that has 20 rows with 30000 variables? And that you want to add one row with 27 variables?

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 2 Nov 2020
Edited: Adam Danz on 30 Dec 2023
Update: You can use paddata starting in MATLAB R2023b to pad tables.
Padding & horizontally concatenating tables
As of r2020b, padarray does not support table inputs. Use outerjoin() to horizontally concatenate and pad tables with different numbers of rows (or columns).
Here's a demo
% Table T1 is 5x3
% Table T2 is 3x4
T1 = array2table(rand(5,3),'VariableNames',{'a' 'b' 'c'})
T1 = 5x3 table
a b c _______ ________ _______ 0.48834 0.037879 0.90083 0.331 0.40246 0.78311 0.86148 0.41245 0.24863 0.42455 0.049638 0.46388 0.99029 0.57911 0.71199
T2 = array2table(rand(3,4),'VariableNames',{'d', 'e' 'f', 'g'})
T2 = 3x4 table
d e f g _______ ________ ________ _______ 0.45315 0.083668 0.5282 0.37319 0.81502 0.98497 0.33396 0.999 0.70467 0.35012 0.029792 0.81679
% Add temporary key to each table
T1.KEY = (1:height(T1))'
T1 = 5x4 table
a b c KEY _______ ________ _______ ___ 0.48834 0.037879 0.90083 1 0.331 0.40246 0.78311 2 0.86148 0.41245 0.24863 3 0.42455 0.049638 0.46388 4 0.99029 0.57911 0.71199 5
T2.KEY = (1:height(T2))'
T2 = 3x5 table
d e f g KEY _______ ________ ________ _______ ___ 0.45315 0.083668 0.5282 0.37319 1 0.81502 0.98497 0.33396 0.999 2 0.70467 0.35012 0.029792 0.81679 3
% Join tables
T = outerjoin(T1,T2, 'Keys', 'KEY')
T = 5x9 table
a b c KEY_T1 d e f g KEY_T2 _______ ________ _______ ______ _______ ________ ________ _______ ______ 0.48834 0.037879 0.90083 1 0.45315 0.083668 0.5282 0.37319 1 0.331 0.40246 0.78311 2 0.81502 0.98497 0.33396 0.999 2 0.86148 0.41245 0.24863 3 0.70467 0.35012 0.029792 0.81679 3 0.42455 0.049638 0.46388 4 NaN NaN NaN NaN NaN 0.99029 0.57911 0.71199 5 NaN NaN NaN NaN NaN
% Remove KEY columns
T(:, cumsum([width(T1), width(T2)])) = []
T = 5x7 table
a b c d e f g _______ ________ _______ _______ ________ ________ _______ 0.48834 0.037879 0.90083 0.45315 0.083668 0.5282 0.37319 0.331 0.40246 0.78311 0.81502 0.98497 0.33396 0.999 0.86148 0.41245 0.24863 0.70467 0.35012 0.029792 0.81679 0.42455 0.049638 0.46388 NaN NaN NaN NaN 0.99029 0.57911 0.71199 NaN NaN NaN NaN
  27 Comments
isamh
isamh on 3 Nov 2020
yes, var names are at top but those arent the actually header names
Adam Danz
Adam Danz on 3 Nov 2020
I don't understand. Please attach the file. If the file is too large, delete most of the rows.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 2 Nov 2020
The below code accounts for the possibility of either table having fewer rows than the other one.
Table1 = [Speed_50_1, Speed_50_2, Speed_55_1, Speed_55_2, Speed_60_1, Speed_60_2, Speed_65_1, Speed_65_2, Speed_70];
T_preserve = readtable('Tesla_Steady_State_027.csv','Delimiter',',');
T_preserve{end+1:height(Table1),:} = missing;
Table1{end+1:height(T_preserve), :} = missing;
T_new = [T_preserve, PD];
writetable(T_new, 'Tesla_Steady_State_027.csv');
  16 Comments
isamh
isamh on 6 Nov 2020
thank you, yeah, I decided to save the data on a xlsx file instead of a csv file. do you know how i can add an empty row underneath Table11-Table13 and right on top of Table21-Table23
Adam Danz
Adam Danz on 8 Nov 2020
Check out the 'range' parameter for writetable().

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!