Save number of iterations in a for loop
Show older comments
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
Scott MacKenzie
on 13 Jul 2021
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.
Rajvi Amle
on 13 Jul 2021
Rajvi Amle
on 13 Jul 2021
Scott MacKenzie
on 13 Jul 2021
Edited: Scott MacKenzie
on 13 Jul 2021
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.
Rajvi Amle
on 13 Jul 2021
Scott MacKenzie
on 13 Jul 2021
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.
Rajvi Amle
on 13 Jul 2021
Scott MacKenzie
on 13 Jul 2021
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.
Rajvi Amle
on 13 Jul 2021
Scott MacKenzie
on 13 Jul 2021
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.
Scott MacKenzie
on 13 Jul 2021
Sorry but I don't have any further suggestions. Good luck.
Rajvi Amle
on 14 Jul 2021
Scott MacKenzie
on 14 Jul 2021
@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.
Accepted Answer
More Answers (1)
Scott MacKenzie
on 14 Jul 2021
Edited: Scott MacKenzie
on 14 Jul 2021
@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
Rajvi Amle
on 14 Jul 2021
Edited: Rajvi Amle
on 14 Jul 2021
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!