How to convert arrays out of cells

조회 수: 1 (최근 30일)
Oliver Makan
Oliver Makan 2019년 11월 14일
편집: Adam Danz 2019년 11월 15일
Hi Guys,
I wanted to convert the fields of the structure into variables to correlate them with each other.
Manually I just click on them an transfer them into workspace as a cell array. Like you see at the bottom I get a code like this with incremental values.
Is there a possibilitiy to do this with an for loop or something like this that it generates my cells automatically out of the structure.
Thank you for your help.
000003.jpg

답변 (1개)

Adam Danz
Adam Danz 2019년 11월 14일
편집: Adam Danz 2019년 11월 15일
Generating variables within a loop involves dynamic variable naming which should be avoided at all cost. Instead, since your arrays appear to be all the same size, it would be much better to pack them into a matrix or a cell array. A matrix would make it very easy to compute correlations between variables. For example, corrcoef(A) computes correlation coefficients for each column of A. Here's a demo using a structure that is similar to yours.
% Create structure
SAM.Inn(1).Ri = rand(1,100);
SAM.Inn(1).Diff = rand(1,100);
SAM.Inn(2).Ri = rand(1,100);
SAM.Inn(2).Diff = rand(1,100);
SAM.Inn(3).Ri = rand(1,100);
SAM.Inn(3).Diff = rand(1,100);
SAM.Inn(4).Ri = rand(1,100);
SAM.Inn(4).Diff = rand(1,100);
% Put all "Ri" into a matrix where each column is a field from SAM.Inn
Ri = cell2mat({SAM.Inn.Ri}.'); % Or, see Stephen's comment below.
% Compute correlation coefficients
cc = corrcoef(Ri);
If you'd rather work with cell arrays or if your data do not have the same number of observations,
Ri = {SAM.Inn.Ri};
  댓글 수: 1
Stephen23
Stephen23 2019년 11월 15일
Without the intermediate cell array and slow cell2mat:
Ri = vertcat(SAM.Inn.Ri).';

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

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by