Subplotting programmatically for varying amounts of subplots
9 views (last 30 days)
Show older comments
Hi, It may be that there is terminology or expressions I am not aware of and that this question has already been answered but I cannot find it. I have a number of files to deal with, usually 4, which I can competently subplot in a loop. If a=the number of files then i designated the subplot area as rows=a/2 and columns = a. This is fine for when I have four files, what I do not know how to handle is the case where a =3 or 5.
I would like to know how to approach creating another figure when necessary and to handle the cases where a is less than 4.
I now I could just plot more graphs on a single page but they would be unreadable once transferred into the pdf report generator I am using.
2 Comments
Jan
on 5 Apr 2017
If n is the number of diagrams, your figure should contain n/2 rows and n columns? Do you mean n/2 columns?
Accepted Answer
Jan
on 5 Apr 2017
Edited: Jan
on 5 Apr 2017
If your goal is to have up to 2x2 suplots on each figure:
Folder = 'C:\Temp\';
FileList = dir(fullfile(Folder, '*.mat')); % Or whatever
for iFile = 1:numel(FileList)
data = load(fullfile(Folder, FileList(iFile).name);
index = mod(iFile - 1, 4) + 1; % [EDITED] mod(iFile, 4) is not sufficient
if index == 1 % Create a new figure
FigH = figure;
end
AxesH = subplot(2, 2, index, 'Parent', FigH);
plot(data.x, data.y); % Adjust to your needs of course
end
More Answers (0)
See Also
Categories
Find more on Subplots 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!