how to convert structure array into matrix

조회 수: 24 (최근 30일)
mahya pv
mahya pv 2018년 8월 2일
편집: Adam Danz 2018년 8월 2일
i want to convert structure array into matrix but i get error while running this code in matlab2016 and 2017: haxes = cell2mat(struct2cell(my structure));
the error was: CELL2MAT does not support cell arrays containing cell arrays or objects.
is there anyone who can help me?
  댓글 수: 1
jonas
jonas 2018년 8월 2일
Can you upload the struct as a .mat file?

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

답변 (1개)

Adam Danz
Adam Danz 2018년 8월 2일
편집: Adam Danz 2018년 8월 2일
I assume the fields of your structure, 's' are cell arrays and that all of the cell arrays have the same size and are row vectors. If the fields are column vectors, some tiny adjustments will be needed.
Under these assumptions, here are two (admittedly weird) ways to convert 's' to a matrix. Simpler methods may exist.
% Fake data
s.f1 = {1 2 3};
s.f2 = {4 3 2};
Given these fake data, cell2mat(struct2cell(s)); will throw the error your described.
Option 1
sm = cell2mat(table2array(cell2table(struct2cell(s))));
Option 2
sm = cell2mat(reshape(struct2array(s), [], numel(fieldnames(s)))');
Option 2 is better.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by