Splitting table field with multiple rows into separate columns
30 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Chris
2023-5-3
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{:,1}{1}
Does that help?
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!