How to save all opened window Figure (imshow) with specified name and folder?

조회 수: 4 (최근 30일)
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

채택된 답변

Geoff Hayes
Geoff Hayes 2019년 7월 12일
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).
  댓글 수: 1
Diah Junaidi
Diah Junaidi 2019년 7월 13일
sir, what about if we wanna run another file and the files that we have already run in the folder will not replace by the new one. I mean... for first compile, we've got 7 images with each their names and they stored in folder named "Hasil" .... later then we going to run another 7 files and wanna store them with each their names into folder named "Hasil". It's so challenging I thought. So fun if we've done this challenge.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Jon
Jon 2019년 7월 12일
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.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by