필터 지우기
필터 지우기

How to create new rows in a table based on nested cell arrays of different sizes

조회 수: 23 (최근 30일)
So I have a table that contains nested cell arrays in only one column. What I'd like to accomplish is create a new table that has the contents of each cell made into new rows below the original row.
Ive attached a sample of what the data looks like(file), as well as what id like to accomplish(file2). I'd like to accomplish this for every embedded cell.

채택된 답변

Stephen23
Stephen23 2023년 6월 22일
편집: Stephen23 2023년 6월 22일
This is MATLAB, so do not use on concatenation within loops to achieve this task. Better:
S1 = load('file.mat');
S2 = load('file2.mat');
T1 = S1.non_compliance_B % provided input
T1 = 9×9 table
ID PayloadElectronicsUnitSubsystemSpecification LastModifiedOn ProposedVerifDescription ProposedText ObjectType ChildTraceabilityCount ChildTraceability RvCCompliance __________ ____________________________________________ ______________ ________________________ ____________ __________ ______________________ _________________ _____________________ "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {16×1 cell} "Marginal Compliance" "PEU-389" {0×0 cell} 27-Feb-2023 {0×0 char} {0×0 char} {'Req'} 9 { 9×1 cell} "Non-Compliance" "PEU-840" {0×0 cell} 27-Feb-2023 {0×0 char} {0×0 char} {'Req'} 33 {33×1 cell} "Marginal Compliance" "PEU-280" {0×0 cell} 28-Feb-2023 {0×0 char} {0×0 char} {'Req'} 8 { 8×1 cell} "Non-Compliance" "PEU-2595" {0×0 cell} 16-Apr-2023 {0×0 char} {0×0 char} {'Req'} 5 { 5×1 cell} "Non-Compliance" "PEU-2591" {0×0 cell} 16-Apr-2023 {0×0 char} {0×0 char} {'Req'} 5 { 5×1 cell} <missing> "PEU-730" {0×0 cell} 27-Feb-2023 {0×0 char} {0×0 char} {'Req'} 6 { 6×1 cell} "Non-Compliance" "PEU-2586" {0×0 cell} 16-Apr-2023 {0×0 char} {0×0 char} {'Req'} 5 { 5×1 cell} "Non-Compliance" "PEU-956" {0×0 cell} 27-Feb-2023 {0×0 char} {0×0 char} {'Req'} 6 { 6×1 cell} "Non-Compliance"
T2 = S2.bal % desired output
T2 = 24×9 table
ID PayloadElectronicsUnitSubsystemSpecification LastModifiedOn ProposedVerifDescription ProposedText ObjectType ChildTraceabilityCount ChildTraceability RvCCompliance __________ ____________________________________________ ______________ ________________________ ____________ __________ ______________________ _________________ _____________________ "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'PRS-738' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'PRS-762' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'PRS-910' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'RF1-553' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'RF1-560' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'RFIO-993' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'RFIO-1000'} "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'RFIO-1002'} "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'RFIO2-147'} "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'RFIO2-156'} "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'RFIO2-294'} "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'2566' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'2582' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'2583' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'2584' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'2585' } "Marginal Compliance"
L = cellfun(@numel,T1.ChildTraceability);
T3 = repelem(T1,L,1);
T3.ChildTraceability = vertcat(T1.ChildTraceability{:})
T3 = 93×9 table
ID PayloadElectronicsUnitSubsystemSpecification LastModifiedOn ProposedVerifDescription ProposedText ObjectType ChildTraceabilityCount ChildTraceability RvCCompliance __________ ____________________________________________ ______________ ________________________ ____________ __________ ______________________ _________________ _____________________ "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'PRS-738' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'PRS-762' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'PRS-910' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'RF1-553' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'RF1-560' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'RFIO-993' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'RFIO-1000'} "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'RFIO-1002'} "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'RFIO2-147'} "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'RFIO2-156'} "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'RFIO2-294'} "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'2566' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'2582' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'2583' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'2584' } "Marginal Compliance" "PEU-2069" {0×0 cell} 05-May-2023 {0×0 char} {0×0 char} {'Req'} 16 {'2585' } "Marginal Compliance"
  댓글 수: 2
Peter Perkins
Peter Perkins 2023년 7월 17일
Stephen23 is 100% correct, but in case anyone needs an explanation of this somewhat advanced trick:
1) each cell of ChildTraceability contains a column with multiple rows. Nothing wrong with that, it's how you "multiple rows in one row of a table".
2) To move those "inner" rows out to the table itself, we need to make copies of each of T1's rows so there is one row for each of the "inner rows". That's what the repelem does. But it also makes copies of each of the cells in ChildTraceability, and we want to "explode" them instead.
3) So turn ChildTraceability into a cell column with one char row in each cell by first applying {:} to get all the contents of all the cells, and then vertcat to put those into one column of cells. This takes some thought, because ChildTraceability is a cell array each of whose cells contains a cell array (or char row vectors). Strings (i.e. double quotes) would make this easier to think about!
4) I might go one step further and turn ChildTraceability into a categorical variable, but not if its values are all unique

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Akira Agata
Akira Agata 2023년 6월 22일
How about the following?
load("file.mat");
bal = table();
for kk = 1:height(non_compliance_B)
t = non_compliance_B(kk, :);
t = t(ones(t.ChildTraceabilityCount, 1), :);
t.ChildTraceability = t.ChildTraceability{1};
bal = [bal; t]; %#ok
end
  댓글 수: 3

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by