MATLAB: Trying to pad smaller table to larger table

concatenatecsvpadtable

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');

Best Answer

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
Related Question