Pad a table without variable names
57 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Adam Danz
2019-11-12
编辑:Adam Danz
2023-12-30
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'})
% Create pad table
Tpad = array2table(zeros(T.Frame(1)-1,size(T,2)),'VariableNames',T.Properties.VariableNames)
% Concatenate tables
Tcat = [Tpad;T]
2 个评论
Aritra Chatterjee
2020-5-15
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
2020-5-15
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];
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Identification 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!