How can I save each images in a loop for?

5 views (last 30 days)
Hi everyone,
I need to save each images product in a ciclo for. How can I do this?
When I tried to do it, it appears:
"Error using plot_matrix
Too many output arguments."
Do you have any advices?
  1 Comment
Vilém Frynta
Vilém Frynta on 8 Feb 2023
Edited: Vilém Frynta on 8 Feb 2023
It would be helpful to see your code, so we can help you better.
Without seeing your code, I can advise you to to create strings that change in each loop and then use these strings as new names to save images.
Something like...
for q = 1:10
name = strcat("img",num2str(q)) % create name; results will be img1, img2,..
plot(x(q),y(q)) % plot your things
print(gcf,name,'-dpng','-r300'); % save plot (or use imwrite if it's an image)
end

Sign in to comment.

Answers (1)

Dongyue
Dongyue on 16 Feb 2023
clear; close all; clc;
for i = 1:5
img_name = ['img',num2str(i),'.jpg'];
img = rand(25,25,3);
imwrite(img,img_name)
end
You can go through the documentation for imwrite() for more information about saving images:
  1 Comment
Walter Roberson
Walter Roberson on 16 Feb 2023
I would probably code either
img_name = sprintf('img%d.jpg', i);
or else
img_name = "img" + i + ".jpg";
When you use double-quoted strings and the + operator then the number in i (and up to 4 decimal places) will be converted to text automatically without needing to explicitly convert the number.

Sign in to comment.

Categories

Find more on Images in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!