How to convert cell to a struct with fields and multiple dimensions?

조회 수: 13 (최근 30일)
Maria Hart
Maria Hart 2021년 1월 21일
댓글: Maria Hart 2021년 1월 22일
Dear all,
I have a cell (P_ungedeckt_h) with multiple dimensions:
P_ungedeckt_h is a 2x1 matrix each containing a struct:
This struct is in turn is devided in fields containing a 8760x1 vector:
I would now like to create from this a variable which is called P_ungedeckt and contains P_ungedeckt.el which is a vector 17520x1 with all .el values below eacht other and a P_ungedeckt.th with the same.
I got this so far,
P_ungedeckt = cell2mat(P_ungedeckt_h);
however it is not quite what I want as it gives me this:
But my goal is not to have the two rows each containing a 8760x1 double. I wish to have 17520 ( = 2x8760) rows, so first the 8760 values of the first double and then the next 8760 beneath that.
I hope this is understandable.
With kind regards
Maria
  댓글 수: 2
Yvan Lengwiler
Yvan Lengwiler 2021년 1월 21일
I am not sure I filly understand what you are after, but let me try.
Let me first create a few dummy variables that have the same structure as what you work with:
th1 = (1:5)';
el1 = 10 * th1;
th2 = (6:10)';
el2 = 10 * th2;
s1 = struct('th',th1,'el',el1);
s2 = struct('th',th2,'el',el2);
s = {s1,s2};
s is your cell array of structs (P_ungedekt_h).
Now you want to create, using only s, a new struct with two components, containing all the data aggregated:
s = struct('th',[s{1}.th;s{2}.th],'el',[s{1}.el;s{2}.el]);
This produces, I think, what you are after:
>> disp(s)
th: [10×1 double]
el: [10×1 double]
Image Analyst
Image Analyst 2021년 1월 21일
Yvan, this looks like an Answer to me, so it should be an official "Answer" down below rather than up here in the comments section which is used to ask the poster for clarification or to attach missing files or code. You can even get "credit" if you post it in the Answers section.

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

채택된 답변

Jon
Jon 2021년 1월 21일
편집: Jon 2021년 1월 21일
I think this would do what you are looking for
% example make up some data just to try it
P_ungedeckt_h = cell(2,1);
for k = 1:2
P_ungedeckt_h{k,1}.th = rand(8760,1);
P_ungedeckt_h{k,1}.el = rand(8760,1);
end
% make the desired structure
P_ungedeckt.el = [P_ungedeckt_h{1,1}.el;P_ungedeckt_h{2,1}.el];
P_ungedeckt.th = [P_ungedeckt_h{1,1}.th;P_ungedeckt_h{2,1}.th];
  댓글 수: 3
Jon
Jon 2021년 1월 21일
편집: Jon 2021년 1월 21일
There may be a more elegant way to do this, but I think this will work
% get some dimensions
p = size(P_ungedeckt_h,1);
% define separate cell arrays one for el's and one for
% th's to temporarily hold values
% first preallocate cell arrays
el = cell(p,1);
th = cell(p,1);
% loop to assign values
for k = 1:p
el{k} = P_ungedeckt_h{k,1}.el;
th{k} = P_ungedeckt_h{k,1}.th;
end
% concatenate cell array values
P_ungedeckt.el = vertcat(el{:});
P_ungedeckt.th = vertcat(th{:});

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

추가 답변 (0개)

카테고리

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