Pad a table without variable names
조회 수: 60 (최근 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!