- Extract the required portion of data from the 'Var10' column (from the 2nd to the 15th element and optionally beyond for subsequent rows).
- Create new variables ('Var11', 'Var12', etc.) corresponding to the extracted values.
- Construct a new table using the extracted values and keep the first row as the base.
- Remove the rows which are part of these new variables
rearrange data in table
5 views (last 30 days)
Show older comments
I have a table and I want to rearrange some data but I don't know how.
The table i work with has 10 variables and around 1000 rows.
I want to put the second element till fifteenth element of Var10 in the first row and then delete the rest of the columns, for exeample:
Old table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10
20/06/2015 09:26:18 2 1218 322 33 3 2 33.22 33.22
20/06/2015 09:26:18 2 1218 322 33 3 2 23.33 23.33
20/06/2015 09:26:18 2 1218 322 33 3 2 43.22 43.22
20/06/2015 09:26:18 2 1218 322 33 3 2 13.33 13.33
20/06/2015 09:26:18 2 1218 322 33 3 2 93.22 93.22
.......................
New table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var24
20/06/2015 09:26:18 2 1218 322 33 3 2 33.22 33.22 23.33 43.22 13.33 93.22 .......
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8 Var9 Var10 Var11 Var12 Var13 Var14 Var24
20/06/2015 09:26:18 2 1218 322 33 3 2 88.66 46.33 57.21 57.33 72.11 55.10 ...... (16th row of original table)
What is the best way to do this?
0 Comments
Answers (1)
Shantanu Dixit
on 17 Jan 2025
Edited: Shantanu Dixit
on 17 Jan 2025
Hi Niet,
I understand that you want to extract elements from a column and construct a new table using the extracted values removing the in between rows. You can achieve the desired functionality by:
% Assuming table stored as oldTable
% With columns named as Var1, Var2, ....
% Extract the second to fifteenth elements of Var10
Var10Data = OldTable.Var10(2:15);
% Creating new table with new variables Var11, Var12, ..., Var24 for extracted data
newTable = oldTable(1, :);
newVars = array2table(Var10Data', 'VariableNames', strcat('Var', string(11:10+length(Var10Data))));
newTable = [NewTable NewVars];
% Rows from 16 onwards
% Append to NewTable NewTable = [NewTable; RemainingRows];
remainingRows = oldTable(16:end, :);
newTable = [newTable; remainingRows];
You can refer to following MathWorks documentation for more information:
"array2table": https://www.mathworks.com/help/matlab/ref/array2table.html
Hope this helps!
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!