save images inside for loop

6 Ansichten (letzte 30 Tage)
MechenG
MechenG am 21 Okt. 2024
Beantwortet: Walter Roberson am 22 Okt. 2024
In the below for loop, i runs from 1 3 5 7 ....100. But I would like to save images Y1.bmp, Y2.bmp, Y3. bmp .... instead of Y1.bmp, Y3.bmp, Y5. bmp. Is there any way to implement this in the below for loop
for i = 1:2:100
%% operation %%
saveas(gcf,sprintf('Y_%d.bmp',i))
end

Akzeptierte Antwort

R
R am 21 Okt. 2024
You can achieve this by using an additional counter that tracks the number of images saved.
Here’s a modified version of your loop:
imageCounter = 1; % Initialize a counter for saved images
for i = 1:2:100
%% operation %%
saveas(gcf, sprintf('Y_%d.bmp', imageCounter));
imageCounter = imageCounter + 1; % Increment the counter
end
This way, you'll save images as Y_1.bmp, Y_2.bmp, Y_3.bmp, and so on, regardless of the values of i.
Hope this helps!
  1 Kommentar
MechenG
MechenG am 22 Okt. 2024
Thank you very much!. This was exactly I am looking for it.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 22 Okt. 2024
for i = 1:2:100
%% operation %%
saveas(gcf,sprintf('Y_%d.bmp',(i+1)/2))
end

Kategorien

Mehr zu Images finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by