Save number of iterations in a for loop

I have a for loop, but every iteration overwrites the variable and I have only the final data left.. how can I save data from every loop? Following is my code:
n=10;
t=[];
x=[];
for i=1:n
tspan=[0 max(EXP.t)];
MAT=[SYSTEM.m_BW(i) SYSTEM.w_L(i) SYSTEM.w_K(i) SYSTEM.w_Lu(i) SYSTEM.w_BR(i) SYSTEM.w_Blood(i) SYSTEM.w_Plasma(i) SYSTEM.m_L(i) SYSTEM.m_BR(i) SYSTEM.m_K(i) SYSTEM.m_S(i) SYSTEM.m_Lu(i) SYSTEM.m_Blood(i) SYSTEM.m_Plasma(i)...
SYSTEM.V_L(i) SYSTEM.V_BR(i) SYSTEM.V_K(i) SYSTEM.V_S(i) SYSTEM.V_Lu(i) SYSTEM.V_Blood(i) SYSTEM.V_Plasma(i) SYSTEM.F_C(i) SYSTEM.F_L(i) SYSTEM.F_BR(i) SYSTEM.F_K(i) SYSTEM.F_S(i) SYSTEM.F_Res(i) SYSTEM.F_bal(i) SYSTEM.F_Bile(i) SYSTEM.F_Urine(i)...
SYSTEM.V_Res(i) SYSTEM.V_bal(i) SYSTEM.V_L_b(i) SYSTEM.V_L_t(i) SYSTEM.V_BR_b(i) SYSTEM.V_BR_t(i) SYSTEM.V_K_b(i) SYSTEM.V_K_t(i) SYSTEM.V_S_b(i) SYSTEM.V_S_t(i) SYSTEM.V_Lu_b(i) SYSTEM.V_Lu_t(i) SYSTEM.V_Res_b(i) SYSTEM.V_Res_t(i)...
DRUG.m_Au_iv(i) DRUG.M_Au_iv(i)]';
options=odeset('AbsTol',10e-2,'RelTol',10e-2,'Stats','on');
col=zeros(18,1);
[t0,x0]=ode15s(@ode_toy, tspan, col,[],ov,DRUG,MAT);
t=[t, {t0}];
x=[x, {x0}];
end
saves only the last value when i=10. how do I get the other values for (t) and (x)?
Any help would be really appreciated. Thank You in advance.

13 Comments

I had posted an answer, but on further examination, I don't see any issue in your code concerning the values for t and x. They are built-up in an array with each pass in the loop and should not contain just the last value at the end of the loop.
What do you see if you type whos in the command window after your code executes?
BTW, your code can't be executed due to the use of unitialized variables.
@Scott MacKenzie Thank you so much for your response. Basically, what I want to do is I want to simulate 10 iterations, save each of the iteration in a loop and then plot these 10 iterations as a whole after the loop. Based on these 10 iterations, I want to calculate the mean and confidence interval and plot for the same. But what majorly is happening that, my loop runs properly with the code, showed 10 lines in the plot, when I posted my question, however when I tried to calculate and plot its mean and CI, then the plot changes along with the iterations going on in the loop and at the end, it will only show the plot for the 10th iteration, the last iteration (i=10). Instead I want a single plot as a whole that shows mean for all the plotted 10 iterations.
@Scott MacKenzie Also, typing whos gives me values as follows with my original code:
n=10;
t=[];
x=[];
for i=1:n
tspan=[0 max(EXP.t)];
MAT=[SYSTEM.m_BW(i) SYSTEM.w_L(i) SYSTEM.w_K(i) SYSTEM.w_Lu(i) SYSTEM.w_BR(i) SYSTEM.w_Blood(i) SYSTEM.w_Plasma(i) SYSTEM.m_L(i) SYSTEM.m_BR(i) SYSTEM.m_K(i) SYSTEM.m_S(i) SYSTEM.m_Lu(i) SYSTEM.m_Blood(i) SYSTEM.m_Plasma(i)...
SYSTEM.V_L(i) SYSTEM.V_BR(i) SYSTEM.V_K(i) SYSTEM.V_S(i) SYSTEM.V_Lu(i) SYSTEM.V_Blood(i) SYSTEM.V_Plasma(i) SYSTEM.F_C(i) SYSTEM.F_L(i) SYSTEM.F_BR(i) SYSTEM.F_K(i) SYSTEM.F_S(i) SYSTEM.F_Res(i) SYSTEM.F_bal(i) SYSTEM.F_Bile(i) SYSTEM.F_Urine(i)...
SYSTEM.V_Res(i) SYSTEM.V_bal(i) SYSTEM.V_L_b(i) SYSTEM.V_L_t(i) SYSTEM.V_BR_b(i) SYSTEM.V_BR_t(i) SYSTEM.V_K_b(i) SYSTEM.V_K_t(i) SYSTEM.V_S_b(i) SYSTEM.V_S_t(i) SYSTEM.V_Lu_b(i) SYSTEM.V_Lu_t(i) SYSTEM.V_Res_b(i) SYSTEM.V_Res_t(i)...
DRUG.m_Au_iv(i) DRUG.M_Au_iv(i)]';
options=odeset('AbsTol',10e-2,'RelTol',10e-2,'Stats','on');
col=zeros(18,1);
[t0,x0]=ode15s(@ode_toy, tspan, col,[],ov,DRUG,MAT);
t=[t, {t0}];
x=[x, {x0}];
end
CF 1x1 352 struct
DRUG 1x1 3992 struct
EXP 1x1 4888 struct
MAT 46x1 368 double
SYSTEM 1x1 15032 struct
col 18x1 144 double
i 1x1 8 double (shows only i=10)
t 1x10 11928 cell
t0 135x1 1080 double
tspan 1x2 16 double
x 1x10 197024 cell
x0 135x18 19440 double
OK, so t and x do contain the values for all 10 iterations of the loop. For t ...
Access the 1st and 2nd elements, respectively, using
t{1}
t{2}
I'm not sure what t contains except to note that it is a cell array.
Again, your code can't be executed at this end, because of it accesses unitialized variables.
@Scott MacKenzie I didn't post defined variables because there 46 variables and 18 ODE equations which would be very complex.However, I am attaching the .m files toy_example, ode_toy and ae_toy which are interconnected to each other. The loop is given in toy_example.m file. But when checked by myself, for 1 simulation, the scenerio is, t is a time vector for n time steps in 6x1 dimensions. x is output of 2 dynamic states of size 6x2 dimension. but for 10 simulations, t and x for 18 dynamic states are stored in 1x10 cell (146x18 values). I dont know if it is correct or not.
Hmm, there is a lot to digest here. How about this. Add the following line at the end of your code:
save('savedata.mat', 't', 'x');
and then post the saved t and x variables via savedata.mat.
@Scott MacKenzie Sorry I did not get what you suggested. Could you please explain to me that again?
OK, I'll try to explain further. Currently, the code in your question ends with...
...
x=[x, {x0}];
end
Add one line:
...
x=[x, {x0}];
end
save('savedata.mat', 't', 'x');
When you run the code, the data in the variables t and x will be saved in savedata.mat. Then post that file here, so the data in t and x can be examined.
@Scott MacKenzie Okay. Done as suggested. Here I have attached the file 'savedata.mat'.
OK, I can see what's in the t and x variables. It should be relatively easy to iterate through t and x, pull out the data, analyses them, plot them, etc.
So that I can understand your goal, let's just condider the data for one iteration -- say, the 1st iteration. There are 137 t values. This is your time vector. The values start at 0 and end at 24. The x variable is a 137x18 double matrix. I assume each row corresponds to a value in t. The 18 column means range from 0.000015 to 0.06829. That's where my understanding ends. These numbers mean nothing to me, and please understand that I can't spend a huge amount of time studying your scripts and research topic. But, I can try to help a bit.
Can you please describe the organization of the values in x and the sorts of analyses you want and as well as the desired plots. If you have any example plots to share, that would help as well.
Sorry but I don't have any further suggestions. Good luck.
@Scott MacKenzie Its okay Sir. Anyways thank you so much for your help and time.
@Rajvi Amle Now that we've established that the t and x data are indeed collected in your script, I've posted an answer demonstrating one way to extract the data from the variables and plot the results for the 10 simulations. There might be different or more appropriate ways to aggregate the data or compute summary statistics, but I'll leave this for you to explore. Good luck.

Sign in to comment.

 Accepted Answer

An easy solution is:
n=10;
for i=1:n
tspan=[0 max(EXP.t)];
MAT=[SYSTEM.m_BW(i) SYSTEM.w_L(i) SYSTEM.w_K(i) SYSTEM.w_Lu(i) SYSTEM.w_BR(i) SYSTEM.w_Blood(i) SYSTEM.w_Plasma(i) SYSTEM.m_L(i) SYSTEM.m_BR(i) SYSTEM.m_K(i) SYSTEM.m_S(i) SYSTEM.m_Lu(i) SYSTEM.m_Blood(i) SYSTEM.m_Plasma(i)...
SYSTEM.V_L(i) SYSTEM.V_BR(i) SYSTEM.V_K(i) SYSTEM.V_S(i) SYSTEM.V_Lu(i) SYSTEM.V_Blood(i) SYSTEM.V_Plasma(i) SYSTEM.F_C(i) SYSTEM.F_L(i) SYSTEM.F_BR(i) SYSTEM.F_K(i) SYSTEM.F_S(i) SYSTEM.F_Res(i) SYSTEM.F_bal(i) SYSTEM.F_Bile(i) SYSTEM.F_Urine(i)...
SYSTEM.V_Res(i) SYSTEM.V_bal(i) SYSTEM.V_L_b(i) SYSTEM.V_L_t(i) SYSTEM.V_BR_b(i) SYSTEM.V_BR_t(i) SYSTEM.V_K_b(i) SYSTEM.V_K_t(i) SYSTEM.V_S_b(i) SYSTEM.V_S_t(i) SYSTEM.V_Lu_b(i) SYSTEM.V_Lu_t(i) SYSTEM.V_Res_b(i) SYSTEM.V_Res_t(i)...
DRUG.m_Au_iv(i) DRUG.M_Au_iv(i)]';
options=odeset('AbsTol',10e-2,'RelTol',10e-2,'Stats','on');
col=zeros(18,1);
[t0,x0]=ode15s(@ode_toy, tspan, col,[],ov,DRUG,MAT);
time{i}=t0;
X{i}=x0;
end

4 Comments

@Sulaymon Eshkabilov It was established in earlier comments that the problem articulated in this question actually does not occur. In @Rajvi Amle's script, all the data are indeed collected in the t and x variables. This is seen in the output of whos in one of the comments.
The proposed answer does work and collect all solutions in a cell array format.
Yes, that's true. But, so too does the original script.
Just to see all iteration values the collected cell array can be converted into a matrix array after the loop, e.g:
...
XX = cell2mat(X);
Time=cell2mat(time);

Sign in to comment.

More Answers (1)

@Rajvi AmleThis script shows one approach to plotting the results of your simulations. The data loaded are the t and x data collected in the script in your question. I've organized the data from each simulation into 3 (6x1) dimensions as you mentioned in a comment. You can explore other aggregations, as appropriate.
% loads the data for 10 simulations into t and x
load('testdata.mat');
% plot simulation results
tiledlayout(2,5);
for i=1:length(t)
% convert data for each iteration to double matrices
tm = cell2mat(t(i)); % 1 column (time steps)
xm = cell2mat(x(i)); % 18 columns (dynamic states)
% aggregate into 3 columns (6 columns for each dimension)
x3 = [mean(xm(:,1:6),2), mean(xm(:,7:12),2), mean(xm(:,13:18),2)];
nexttile;
plot(tm,x3);
title(sprintf('Simulation #%d', i));
legend('Dim 1', 'Dim 2', 'Dim 3');
end

1 Comment

@Scott MacKenzie and @Sulaymon Eshkabilov Thank you so much for your responses and time to help me out, even my code is bit complex to understand. @Scott MacKenzie I got plots for 10 iterations. Further I want to calculate the mean of all these 10 iterations all together. To make you understand better I.ll explain you here. Following is the part of my code:
for i=1:n
tspan=[0 max(EXP.t)];
MAT=[SYSTEM.m_BW(i) SYSTEM.w_L(i) SYSTEM.w_K(i) SYSTEM.w_Lu(i) SYSTEM.w_BR(i) SYSTEM.w_Blood(i) SYSTEM.w_Plasma(i) SYSTEM.m_L(i) SYSTEM.m_BR(i) SYSTEM.m_K(i) SYSTEM.m_S(i) SYSTEM.m_Lu(i) SYSTEM.m_Blood(i) SYSTEM.m_Plasma(i)...
SYSTEM.V_L(i) SYSTEM.V_BR(i) SYSTEM.V_K(i) SYSTEM.V_S(i) SYSTEM.V_Lu(i) SYSTEM.V_Blood(i) SYSTEM.V_Plasma(i) SYSTEM.F_C(i) SYSTEM.F_L(i) SYSTEM.F_BR(i) SYSTEM.F_K(i) SYSTEM.F_S(i) SYSTEM.F_Res(i) SYSTEM.F_bal(i) SYSTEM.F_Bile(i) SYSTEM.F_Urine(i)...
SYSTEM.V_Res(i) SYSTEM.V_bal(i) SYSTEM.V_L_b(i) SYSTEM.V_L_t(i) SYSTEM.V_BR_b(i) SYSTEM.V_BR_t(i) SYSTEM.V_K_b(i) SYSTEM.V_K_t(i) SYSTEM.V_S_b(i) SYSTEM.V_S_t(i) SYSTEM.V_Lu_b(i) SYSTEM.V_Lu_t(i) SYSTEM.V_Res_b(i) SYSTEM.V_Res_t(i)...
DRUG.m_Au_iv(i) DRUG.M_Au_iv(i)]';
options=odeset('AbsTol',10e-2,'RelTol',10e-2,'Stats','on');
col=zeros(18,1);
[t0,x0]=ode15s(@ode_toy, tspan, col,[],ov,DRUG,MAT,SYSTEM);
t=[t, {t0}];
x=[x, {x0}];
end
After my loop, I have a cell array of x=1x10 cell where each of the 10 arrays are matrices of 138x18. If I want to access the data of each of the cells but just want to calculate the sum of (138,1) and (138,2) from each arrays then how do I calculate and how can I access it? I already tried as shown below:
%% Split state vector and auxiliary vector
% for i=1:n
X = x{1,i}
T = t{1,i}
m_Au_A1=X(:,1); %Amount of AuNP in arterial blood
m_Au_V1=X(:,2); %Amount of AuNP in venous blood
How do I call values stored inside a cell?

Sign in to comment.

Categories

Find more on Read, Write, and Modify Image 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!