Automated simulation with fixed number of simulations

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일
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.

카테고리

도움말 센터File Exchange에서 Scripts에 대해 자세히 알아보기

제품

릴리스

R2021a

태그

질문:

2022년 12월 8일

답변:

2024년 11월 13일

Community Treasure Hunt

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

Start Hunting!

Translated by