How to convert cell to a struct with fields and multiple dimensions?
Show older comments
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 Comments
Yvan Lengwiler
on 21 Jan 2021
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
on 21 Jan 2021
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.
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!