Add multiple plots in the same graph
2 views (last 30 days)
Show older comments
Hello everyone i have the next code which reads multiple pictures from the same folder and in the end plots the fft of the lines of the pics and saves all the fft plots one by one. Is there a way to make show all the ffts of the pics in the same graph and save it? I tried hold on but it just plots in the last pic of the first image the second image.My code is this: ( the plot i want to draw for all my images in the same graph.
clc;
clear all;
% Specify the folder where the files live.
myFolder = 'folder';
% 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\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
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.
% Get a list of all files in the folder, and its subfolders, with the desired file name pattern.
%filePattern = fullfile(myFolder, '**/*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, 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()
img = imread(fullFileName);
%img = imread('1.jpg');
img = imcrop(img)
imshow(img)
figure, imshow(img_rot)
figure, contourf(prof_img_rot)
f = figure;
%figure
loglog(fft_prof(1:128))
hold on
File = sprintf('0.3%0.1f.png', k);
% Write to disk.
exportgraphics(f,fullfile(myFolder,File));
end
hold off
close all
4 Comments
dpb
on 15 Apr 2021
Then take all the calls to figure out of the loop and only create a figure, saving the handle to it before the loop.
Then set hold on and plot all results to the one axes.
For multiple FFTs, this could end up really messy-looking; you might want to look at the waterfall plot to be able to distinguish one from another.
Answers (1)
yanqi liu
on 24 Feb 2022
clc;
clear all; close all;
% Specify the folder where the files live.
myFolder = fullfile(matlabroot,'toolbox/images/imdata');
% 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\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
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.
% Get a list of all files in the folder, and its subfolders, with the desired file name pattern.
%filePattern = fullfile(myFolder, '**/*.png'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
figure;
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, 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()
img = imread(fullFileName);
%img = imread('1.jpg');
%img = imcrop(img);
%imshow(img);
%figure, imshow(img_rot)
%figure, contourf(prof_img_rot)
%f = figure;
%figure
% one image to be saved that has all the subsequent plots
fft_prof = fft2(double(img(:,:,1)));
loglog(fft_prof(1:128))
hold on
%File = sprintf('0.3%0.1f.png', k);
% Write to disk.
%exportgraphics(f,fullfile(myFolder,File));
end
hold off
% close all
0 Comments
See Also
Categories
Find more on Specifying Target for Graphics Output 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!