MATLAB Answers

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

1 view (last 30 days)
Maria Hart
Maria Hart on 21 Jan 2021
Commented: Maria Hart on 22 Jan 2021
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
Image Analyst
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.

Sign in to comment.

Accepted Answer

Jon
Jon on 21 Jan 2021
Edited: Jon on 21 Jan 2021
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 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by