How to save all opened window Figure (imshow) with specified name and folder?
3 views (last 30 days)
Show older comments
Diah Junaidi
on 12 Jul 2019
Commented: Diah Junaidi
on 13 Jul 2019
I already use the code that I found from MathWorks forum. I wanna save all opened figure (imshow) into spesified folder and name. I have no found error but its result isn't that I want it. The result saves the 7 different name which I want to do but all of them are the last figure, meanwhile I wanna each figure with each name. For an example: Figure 1 is stored with name => resultImage_1.jpg
Figure 2 is stored with name => resultImage_2.jpg
% Specify the folder where the files live.
myFolder = 'D:\DIAH\[MATLAB]cv1-fingerspelling-recognition-master\cv1-fingerspelling-recognition-master\imagessss';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.jpg'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
currentImageArray = imread(fullFileName);
currentImage{k}=currentImageArray;
for i = 1 : 7
outputFolder = 'D:\DIAH\[MATLAB]cv1-fingerspelling-recognition-master\cv1-fingerspelling-recognition-master\Hasil Percobaan Lab';
outputFileName = fullfile(outputFolder, ['Hasil Citra Lab_' num2str(i) '.jpg']);
imwrite(currentImage{k}, outputFileName);
% drawnow; % Force display to update immediately.
end
end
0 Comments
Accepted Answer
Geoff Hayes
on 12 Jul 2019
Diah - it isn't clear to me why you have the inner for loop
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
currentImageArray = imread(fullFileName);
currentImage{k}=currentImageArray;
for i = 1 : 7
outputFolder = 'D:\DIAH\[MATLAB]cv1-fingerspelling-recognition-master\cv1-fingerspelling-recognition-master\Hasil Percobaan Lab';
outputFileName = fullfile(outputFolder, ['Hasil Citra Lab_' num2str(i) '.jpg']);
imwrite(currentImage{k}, outputFileName);
% drawnow; % Force display to update immediately.
end
end
but it does explain why you are writing out the same file seven times and why all correspond to the last image that you read in. Note how in the outer loop you iterate over all jpgs (from the folder). Then, for each of these images, you have an inner loop that creates seven files (all with the same image currentImage{k}) across seven files named "Hasil Citra Lab_1.jpg", "Hasil Citra Lab_2.jpg",..., "Hasil Citra Lab_7.jpg". That means that on every iteration of the outer loop, you will overwrite the contents of these seven files (since they don't depend upon the k variable) with the image that has been read on the current iteration of the outer loop.
If you want to make sure that you don't overwrite the files, then you could add k to the file name
outputFileName = fullfile(outputFolder, sprintf('Hasil Citra Lab_%d_%d.jpg', k, i));
Otherwise, you may want to verify if you really need that inner loop (I'm not sure why you need to write the same image to seven different files).
More Answers (1)
Jon
on 12 Jul 2019
The problem is that in your inner loop you set i = 1,2,3...7 but you are writing currentImage{k}. So the index for k does not change, you just write the same file, currentImage{k}, 7 times. It isn't clear to me what the purpose is of this inner loop on index i. Maybe it is not needed. In anycase, you need to look at your loop structures, and make sure that the indices that are being changed are connected to the files you want to write.
0 Comments
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!