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

14 visualizzazioni (ultimi 30 giorni)
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 Commenti
Yvan Lengwiler
Yvan Lengwiler il 21 Gen 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
Image Analyst il 21 Gen 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.

Accedi per commentare.

Risposta accettata

Jon
Jon il 21 Gen 2021
Modificato: Jon il 21 Gen 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 Commenti
Jon
Jon il 21 Gen 2021
Modificato: Jon il 21 Gen 2021
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{:});

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Structures in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by