How to save each image generated in a for loop?
古いコメントを表示
for i=1:20
volume=randi([2 10], 1,1)
location=randi([10 100], 1, 1)
imagegenerator(volume,location)
end
^This is a simplified example of my code.
I generate random variables which are inputted into a function called imagegenerator, which generates an image.
With the for loop, I'm generating several random images.
I want to
1) save each image with a different name each time (i.e "image 1" "image 2" "image 3", etc.)
2) also save the inputs (volume, location) associated with each image (preferably in the same folder/ with the same name "image 1" "image 2" , etc.)
How can I do this? Thanks!
P.S. I want to be able to use all these images/ their inputs later to put into a machine learning algorithm that'll cluster the images based off the pixels or the inputs (not sure yet).
1 件のコメント
Stephen23
2020 年 11 月 2 日
1) use the sprintf method shown here:
2) use saveas:
or download export_fig:
採用された回答
その他の回答 (1 件)
You can print the image:
print('filename','-dpng','-r300')
where '-dpng' can be set to whatever format you want (check out the help file) and '-r300' is the resolution ('-r0' is screen resolution).
To change the filename to image 1 etc you can add the following to your for loop
f_name = ['image ' num2str(i)];
print(f_name,'-dpng','-r300')
Make sure you close your figures after you're done printing them.
Edit: In terms of saving the data, you can just use
save('filename','volume','location')
That will save it as a Matlab .mat file. If you want to write it to file, you should check out "writematrix" or "fprintf".
5 件のコメント
Rachel Dawn
2020 年 11 月 2 日
S. Walter
2020 年 11 月 2 日
It depends on what the function imagegenerator() allows for output.
Function syntax is:
function [x,y] = FunctionName(a,b)
where x and y are the outputs and a and b are the inputs. If the syntax for imagegenerator is:
function imagegenerator(volume,location)
then the function is not set up to give an output. In this case, you would have to add a line somewhere inside the function to grab the figure handle and output that to A:
function A = imagegenerator(volume,location)
A = figure;
plot(volume,location)
end
Then you could change your original loop to:
for i=1:20
volume=randi([2 10], 1,1)
location=randi([10 100], 1, 1)
% Save the image handle
A = imagegenerator(volume,location);
% Make a file name
f_name = ['image ' num2str(i)];
% Save the figure
print(A,f_name,'-dpng','-r150')
% Close the figure
close(A)
% Save the variables to a .mat file
save(f_name,'volume','location')
end
Rachel Dawn
2020 年 11 月 3 日
S. Walter
2020 年 11 月 3 日
You can either try
A = gcf
at the end of that snippet of code (gcf = "get current figure"). This stores the figure handle in A.
Or you can do it before:
% PLOT DENSITIES
% Open a new figure window
A = figure;
% Add a colormap
colormap(gray);
% Display the image - store the handle in A
imagesc(-x);
% Make the axis equal and tight
axis equal;
axis tight;
% Turn the axis off
axis off;
% Pause for a moment
pause(1e-6);
Rachel Dawn
2020 年 11 月 6 日
カテゴリ
ヘルプ センター および File Exchange で Object Detection についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!