How to append structures?

조회 수: 268 (최근 30일)
Vogel
Vogel 2017년 11월 16일
편집: Vogel 2017년 11월 18일
Dear all,
- I use matlab to evaluate some results of daily measurements.
- Each day I run my matlab routine, in order to save the measurements and calculate new variables (e.g. Temperatures, Volume Flows, Energy Flows and Efficiency).
- I would like to create a big matrix or structure containing all the variables, in which every day I can append the new obtained values without deleting the prior ones.
I have in total around 1000 variables, some of them matrixes, some of them vectors, for 200 days of measurements. Most of them have the same length (for the same day), except for some information such as the Date. I am going to try to explain it with a simpler case:
Lets say I get these variables on day 1 and on day 2:
Day1 = struct('Date', 2017/10/21, 'Temperatures', [20 25; 25 30; 27 31; 25 30],'Vol',[ 0.2; 0.4; 0.3; 0.6],'Q',[7; 8;8;9 ]);
Day2 = struct('Date', 2017/10/22, 'Temperatures', [21 22; 23 31],'Vol',[ 0.2; 0.2 ],'Q',[9; 8]);
Since I have more than 1000 variables, I don't do this manually, but using the function ws2struct(), which allows to save all the variables from the current workspace into a struct array. https://de.mathworks.com/matlabcentral/fileexchange/36257-save-workspace-to-struct?focused=5228534&tab=function
I would like to obtain the following matrix, which I should be able to update continuously:
Alldays = struct('Date', ['2017/10/21'; '2017/10/21'; '2017/10/21'; '2017/10/21'; '2017/10/22'; '2017/10/22' ], 'Temperatures', [20 25; 25 30; 27 31; 25 30 ; 21 22; 23 31], 'Vol',[ 0.2; 0.4; 0.3; 0.6 ; 0.2; 0.2],'Q',[7; 8;8;9;9; 8 ]);
- How would you do it? I have tried the following, but it does not work:
Alldata = Day1;
f= fieldnames(Alldata);
for i = 1:length(f)
Alldata.(f{i}) = [Alldata.(f{i}) Day2.(f{i})]
end
Do you have any suggestions? Maybe the struct is not the best way to to this. I am open to other solutions. The important thing is that I can use the data to make new calculations or to depict some graphics, for example, heat flow Q over Temperature T.
Thank you in advance.

답변 (1개)

Guillaume
Guillaume 2017년 11월 16일
For your dates, I would recommend that you store them as datetime rather than char array. From your example it's not clear if your dates are always the same length of 10 characters, which is easy to concatenate vertically, or can sometimes be 9 characters (for days 1 to 9), which is bit more difficult to concatenate. datetime can always be concatenated regardless of the display format.
Anyway, to start with, since it looks like you have a scalar structure per day, you indeed want to concatenate them all into a single variable as you had (before you edited your question)
Alldata = [Day1, Day2, Day3, ...];
You then want to convert that structure array into a scalar structure by vertically concatenating matching fields:
concatenateddata = struct; %final structure
for field = fieldnames(Alldata)'
fname = field{1};
concatenateddata.(fname) = vertcat(Alldata.(fname));
end
  댓글 수: 1
Vogel
Vogel 2017년 11월 16일
편집: Vogel 2017년 11월 18일
Thanks Guillaume, it works! :)
Actually I found an error in my code. I forgot a ';' between Day1 and Day2. Then it works too.
[Alldata.(f{i}); Day2.(f{i})]
------------------------------------------------------------
And just answering your question:
- For a given day, the number of rows of the vectors/matrixes is the same. But the number of columns can be different (e.g. the temperature matrix has always two columns and the Q vector always one).
- From day to day, the number of rows might be different.

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

카테고리

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