Pad a table without variable names
55 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Balázs Szabó
el 12 de Nov. de 2019
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 comentarios
Respuesta aceptada
Adam Danz
el 12 de Nov. de 2019
Editada: Adam Danz
el 30 de Dic. de 2023
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 comentarios
Aritra Chatterjee
el 15 de Mayo de 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
el 15 de Mayo de 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];
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Type Identification en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!