Automated simulation with fixed number of simulations

18 次查看(过去 30 天)
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?

回答(1 个)

Saurabh
Saurabh 2024-11-13,11:10
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.

类别

Help CenterFile Exchange 中查找有关 Scripts 的更多信息

标签

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by