Automated simulation with fixed number of simulations

4 views (last 30 days)
I have written some codes in Matlab. This program will output some figures. I want to automate this simulation. I want to run this program 2000 times and save all the figures generated during simulations. Anybody knows how?

Answers (1)

Saurabh
Saurabh on 13 Nov 2024
Hi @Starry,
To automate MATLAB simulation and save the generated figures, create a script that runs simulation in a loop for 2000 iterations. Here’s a basic outline of how it can be achieved:
% Define the number of iterations
numIterations = 2000;
% Loop over the number of iterations
for i = 1:numIterations
% Run your simulation or function here
% Example: result = mySimulationFunction();
% Assuming your simulation generates a figure
% Get the current figure handle
fig = gcf; % or use figure handle if multiple figures is required
% Define a filename for saving the figure
filename = sprintf('figure_%04d.png', i); % Save as PNG with a unique name
% Save the figure
saveas(fig, filename); % or use print function for more options
% Example: print(fig, filename, '-dpng');
% Close the figure if needed
close(fig);
end
After each simulation run, save the generated figures using the 'saveas' or 'print' function.
I hope this was helpful.

Categories

Find more on Scripts in Help Center and File Exchange

Tags

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!