Clear Filters
Clear Filters

Splitting table field with multiple rows into separate columns

55 views (last 30 days)
Hi,
I have a 459 x 1 table - each row of the table contains n rows of data by x 3 (see attached). Sort of like a nested field.
I would like to rearrange the table so the data appears in separate columns e.g. the first row in my table contains 726 x 3 data points and the second row contains 240 x 3 points. So I'd like to create three separate tables containing 726 x 1, 240 x 1 etc i.e. the first column of data, keeping the row numbers the same. Sorry its so simple but hard to explain!
Vertcat is no good as it just combines and concatenates all the data points in one matrix, which is not what I want. I'd like each row to contain the same data points but the column data to be in separate columns in the table or separate variables, whichever it easiest.
Any help will be most appreciated!

Answers (2)

Chris
Chris on 2 May 2023
Edited: Chris on 2 May 2023
How about:
T = rows2vars(Data);
T(:,1) = [];
Or, if you really only want the first three:
T = table(Data{1,1},Data{2,1},Data{3,1});
This answer may be helpful too.
  1 Comment
Impala
Impala on 3 May 2023
Hi Chris,
Thanks for responding.
Your code reorganises the data into row vectors. What I would like to do is split up the data into 3 columns. So currently, my table contains data in the first column that looks like this:
726x3 double
240x3 double
314x3 double
etc...
I want to split it into 3 columns like this:
row1 col1: 726 x 1 (the first coordinate point)
row1 col2: 726 x 1 (the second coordinate point)
row1 col3: 726 x 1 (the third coordinate point) etc.
Is there a way to do this?
Thanks!

Sign in to comment.


Chris
Chris on 3 May 2023
Whoops. It looks like each array in Data is a cell, so you'll need to drill down a bit. To repack the first cell:
load('Data.mat');
newtable = array2table(Data{1,1}{1});
Breaking it down:
T1 = Data{1,1}; % Get the cell inside this row
T2 = T1{1}; % Access the double array inside the cell
newtable = array2table(T2); % Make a new table
For the first three cells, each has a different number of rows, which doesn't seem to be what tables are meant for. If you want them side by side in a parent table, you'll have to pack them back into cells as far as I can tell.
newtable = table([{array2table(Data{1,:}{1})}, {array2table(Data{2,:}{1})}, {array2table(Data{3,:}{1})}])
newtable = table
Var1 _______________________________________________ {726×3 table} {240×3 table} {314×3 table}
newtable{:,1}{1}
ans = 726×3 table
Var1 Var2 Var3 ________ ________ ________ 0.019608 0.036774 0.053406 0.019302 0.036621 0.053558 0.019455 0.036392 0.053558 0.019379 0.036621 0.05394 0.019684 0.036011 0.053787 0.019531 0.036392 0.054092 0.019455 0.036316 0.053711 0.01915 0.036545 0.053864 0.019455 0.03624 0.053864 0.019455 0.036316 0.053558 0.019379 0.036469 0.053406 0.019608 0.036316 0.053635 0.01915 0.036163 0.053864 0.019684 0.036545 0.05455 0.019836 0.036316 0.054321 0.01915 0.035782 0.053635
Does that help?
fyi, for this kind of arrangement, I prefer working with struct arrays.
  1 Comment
Impala
Impala on 4 May 2023
Hi Chris,
Thanks once again for the help.
That is helpful but what I'm looking to do is split up the contents of each cell into 3 columns. The data I've provided is accelerometer data and I would like each component (x, y, z) in a separate column within the same variable - please see attached pic.
Interestingly, the original data was in a structarray but I thought it might be easier to work in a table. I've attached the struct array data too. Sorry for not being clear about what I'm looking to do!
Thanks!

Sign in to comment.

Categories

Find more on Tables in Help Center and File Exchange

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!