Converting structs within cells to cells

조회 수: 10 (최근 30일)
Layla
Layla 2012년 5월 3일
I have a 20x1 cell (X) where each cell element contains a 16x1 struct. There is only one field throughout any of the structs ('Coordinates', a matrix with 3 columns but varying number of rows). I would like to convert this into a 20x16 cell, where each cell element is the Coordinate matrix. I've tried many different things and nothing gives me quite what I want.
1. squeeze(struct2cell(X{1})) gives me a 16x1 cell where each cell containts a Coordinate matrix. I tried looping through the contents all 20 rows of cell X in hopes of horizontally concatenating the output, and no luck. This also doesn't work:
A = cell(16,20)
for i = 1:20
[A{:,i}] = squeeze(struct2cell(X{i}));
end
The resulting error: Error using squeeze Too many output arguments.
2. Also tried the following:
B = cell2mat(X);
C = cell(16,20);
[C{:}] = B.Coordinates;
This actually did give me a 16x20 cell, but the contents aren't right! I checked the dimensions of the matrices within the cells against the dimensions in the original structs within cells (Variable X), and they don't match!
If anyone could explain what I am doing wrong here, I would really appreciate it!

채택된 답변

Layla
Layla 2012년 5월 3일
Mistake found. Method #2 would work as long as my preallocated cell C was the right size. It should've been cell(20,16). Then transposing the output of [C{:}] = B.Coordinates gives me the answer I need.
Thank you again for your help Walter. This isn't the first question of mine you've helped me on!

추가 답변 (1개)

Walter Roberson
Walter Roberson 2012년 5월 3일
cellfun(@(C) horzcat(C.coordinates), X)
  댓글 수: 1
Layla
Layla 2012년 5월 3일
No luck there either:
Error using horzcat
CAT arguments dimensions are not consistent.
Error in @(C)horzcat(C.ClusterCoordinates)

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by