How to store and access values of multiple nested loops? - 4dimensional
1 view (last 30 days)
Show older comments
Hi guys,
this is my code:
day=140;
Name='winter';
data.battery.QQQ=zeros(10,24,140,number);
bat=[5 10 15 20 25 30 35 40 45 50];
for k=1:length(bat)
C=bat(k);
data.battery.SoC_int=1; %Start.
data.battery.Q=zeros(24*day+1,number); %kWh
data.battery.SoC=zeros(24*day+1,number); %0-1
data.battery.t=zeros(24*day+1,1); %0-1
data.battery.SoC(1,:)=data.battery.SoC_int;
data.battery.Q(1,:)=C(:,1)*data.battery.SoC_int;
data.battery.Check=ones(number,1);
data.PhotoEnergy.(Name)=zeros(24,number);
data.battery.CheckQ=zeros(10,24,140,number);
for i=1:day
for j=1:24
tempor=data.battery.Q((i-1)*24+j,:)+data.PhotoEnergy.(Name)(j,:)-data.ConsumptionEnergy.(Name)(j);
tempor(tempor<0)=0;
tempor(tempor>C(:,1))=C(:,1);
data.battery.Q((i-1)*24+j+1,:)=tempor;
data.battery.t((i-1)*24+j+1,1)=data.battery.t((i-1)*24+j,1)+1;
data.battery.SoC((i-1)*24+j+1,:)=data.battery.Q((i-1)*24+j+1,:)/C(:,1);
data.battery.QQQ(k,j,i,:)=data.battery.Q((i-1)*24+j+1,:);
end
end
end
I would like to store all of the results of data.battery.Q.
Before I added the third for-loop (for k=1:len...) the results got displayed like colums=number and rows=i*j (see white table in picture underneath).
The additional dimension is making it far more difficult.The generated k-loop values are overwritten with the subsequent k-loop values, so that I can only access the results of 'bat=50'.
I'd like to store the results like shown in the next figure:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1005500/image.png)
Among other things, I tried with (as you can see in the code above):
data.battery.QQQ(k,j,i,:)=data.battery.Q((i-1)*24+j+1,:);
But here generated k-loop values are still overwritten with the subsequent k-loop values.
Even though no error occures, I think I made a mistake in the data.battery.QQQ code or something is missing.
A hint could be:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1005505/image.png)
Can anyone help me how to store my generated data as whished?
Thanks a lot in advance,
Toni
0 Comments
Answers (1)
bransa
on 20 May 2022
Could you store the white tables (the results displayed like colums=number and rows=i*j) in a cell array instead? that is something like
data.battery.QQQ = cell(length(bat),1);
for k = 1:length(bat)
...
data.battery.QQQ{k}=data.battery.Q((i-1)*24+j+1,:);
end
0 Comments
See Also
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!