# Splitting table field with multiple rows into separate columns

35 visualizzazioni (ultimi 30 giorni)
Impala il 2 Mag 2023
Commentato: Impala il 4 Mag 2023
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!
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (2)

Chris il 2 Mag 2023
Modificato: Chris il 2 Mag 2023
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});
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Impala il 3 Mag 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!

Accedi per commentare.

Chris il 3 Mag 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:
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 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Impala il 4 Mag 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!

Accedi per commentare.

### Categorie

Scopri di più su Tables in Help Center e File Exchange

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by