Pad a table without variable names

60 views (last 30 days)
Hi,
I'd like to add leading padding to a table in matlab, but I'm having some trouble wiht it.
Let's say I have a table like this:
Frame Value_X Value_Y
__ __ __
7 3.4 2.4
8 2.2 1.4
And I'd like to extend this table, that I read the first value of the Frame column in the table and add rows with zeros in fron of the table so that the row index would match the Frame number (so in this case I'd like to add 6 rows of zeros). I know how to do it if I know all the variable names in the table and I type those in, but I'd like to avoide that(in reality I have much more columns).
Also, each column holds only a single value, so no cells/arrays insinde the table.
And I prefer not to convert the table into an array, because I'd like to use the variable names to acces the values later.
So my question is how can I add leading padding to this table without knowing the variable names in the table

Accepted Answer

Adam Danz
Adam Danz on 12 Nov 2019
Edited: Adam Danz on 30 Dec 2023
Update: use paddata starting in R2023b
This can not be achieved using
Tpad = paddata(T,10,'side','leading')
Before R2023b
Create a table with just 0s and with VariableNames that match your original table. The number or rows in your zeros-table should be determined by the first value in your Frame column. Then vertically concatenate the tables.
% Create demo table
T = table((7:10)',rand(4,1), rand(4,1),'VariableNames',{'Frame','Value_X','Value_Y'})
T = 4×3 table
Frame Value_X Value_Y _____ _______ _______ 7 0.10379 0.56153 8 0.41481 0.5661 9 0.27797 0.80235 10 0.26821 0.10882
% Create pad table
Tpad = array2table(zeros(T.Frame(1)-1,size(T,2)),'VariableNames',T.Properties.VariableNames)
Tpad = 6×3 table
Frame Value_X Value_Y _____ _______ _______ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
% Concatenate tables
Tcat = [Tpad;T]
Tcat = 10×3 table
Frame Value_X Value_Y _____ _______ _______ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 7 0.10379 0.56153 8 0.41481 0.5661 9 0.27797 0.80235 10 0.26821 0.10882
  2 Comments
Aritra Chatterjee
Aritra Chatterjee on 15 May 2020
can i vertically concatenate two seperate tables with different VariableNames ? Basically I wanna write two tables in a CSV file one after another (vertically)
Adam Danz
Adam Danz on 15 May 2020
If the two tables have the same number of columns and the same variable names, vertically concatenate using,
T = [T1; T2];
If the two tables have the same number of columns, same data types in each column, but they have different variable names, first rename the variables in table-2 then vertically concatenate.
T2.Properties.VariableNames = T1.Properties.VariableNames;
T = [T1; T2];

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!