Pad a table without variable names

43 ビュー (過去 30 日間)
Balázs Szabó
Balázs Szabó 2019 年 11 月 12 日
編集済み: Adam Danz 2023 年 12 月 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

採用された回答

Adam Danz
Adam Danz 2019 年 11 月 12 日
編集済み: Adam Danz 2023 年 12 月 30 日
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 件のコメント
Aritra Chatterjee
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
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 ExchangeData Type Identification についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by