How to save following parameters in this loop?

Hi,
I am applying a look for k = 1:500 (shown below) on three paramters ip0, is0, rho0 (each of is 101 * 500) and saving outputs Final_ip, Final_is, Final_rho (of same size lines - 61 - 63). This works iteratively and for each simulation number of iterations (iters in line 53) and hist are diffrent. I want save iters and hist for all 500 simulations/iterations. How can I save it? The size of hist maybe 1*167 and iters is just number of iterations (1 * 1).
If I want know during 500 iterations run that the code is currently on which iteration. How I can check, I mean how code can also show live iteration number/simulation number?

5 Comments

If the size of iters is same for each iteration of the for loop, you can preallocate it and use writematrix to save the data for iters and hist in an excel file.
If the size is not same, preallocate a cell array and use writecell.
Both the write functions mentioned are available from R2019a onwards.
"If I want know during 500 iterations run that the code is currently on which iteration. How I can check, I mean how code can also show live iteration number/simulation number?"
You can use disp to show the iteration counter 'k' in each iteration.
Ahmed
Ahmed on 31 Jan 2024
Edited: Ahmed on 31 Jan 2024
@Dyuman Joshi Thanks for your reply but I am using 2018 version and the size of hist is not same as well. So, how can i solve this in 2018 version?
Preallocate a cell array and store the data in it.
Then you can use padcat to make a numeric array, then use array2table on data to save it via writetable.
The problem is code does not store hist for all iterations. It only store the hist of last iteration. Can you plz help how to formulate the code in this case?
pls share the code , not a picture of it
tx

Sign in to comment.

Answers (1)

Hi Ahmed, 
I understand that you want to save the values of “hist and iters for each iteration in for loop and display the current iteration number. You can do this by using the MATLAB code below:
storage_arr = {}; % create a cell array to save the data
cnt = 1;
for k = 1:10000
% insert your code here
storage.iters = iters; % create a struct to hold the saving data
storage.hist = hist;
storage.k = k;
storage_arr{cnt} = storage; % save the data in the cell array
cnt = cnt+ 1; % counter to track the array index
disp(['Current Iteration is : ',num2str(k)]); % display the current iteration number
end
In this way, you can save the data for each iteration in a cell array of structs. To display the current iteration number, you can use the "disp" function in MATLAB along with the "num2str" function which converts the iteration number to a string.
You can find the link to MathWorks documentation for the functions used in the code below:
I hope this helps!

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 31 Jan 2024

Answered:

on 5 Oct 2024

Community Treasure Hunt

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

Start Hunting!