rearrange data in table

5 views (last 30 days)
Niet hier
Niet hier on 14 Apr 2021
Edited: Shantanu Dixit on 17 Jan 2025
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?

Answers (1)

Shantanu Dixit
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:
  1. Extract the required portion of data from the 'Var10' column (from the 2nd to the 15th element and optionally beyond for subsequent rows).
  2. Create new variables ('Var11', 'Var12', etc.) corresponding to the extracted values.
  3. Construct a new table using the extracted values and keep the first row as the base.
  4. Remove the rows which are part of these new variables
% 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:
Hope this helps!

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!