Subplotting programmatically for varying amounts of subplots

5 views (last 30 days)
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
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?
Stephen Devlin
Stephen Devlin on 5 Apr 2017
hi Jan, I meant the figure containing the subplots will have 2 rows (n/2) and 4(n) columns.
4 is a number of subplots per page that is legible when the subplot figure is saved with print as an .svg and printed in the dom api report generator. So even in the instance where I have less than or more than 4 columns of data I need to subplot per figure I would still only want a maximum of 4 per page. (I apologise if my terminology is poor, am still learning how to 'talk matlab' and probably a little incoherent).

Sign in to comment.

Accepted Answer

Jan
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
  1 Comment
Stephen Devlin
Stephen Devlin on 5 Apr 2017
Hi Jan,
i will try this out, here is the loop I had before
[VolRows,nVolCol] = size(loopDataVol);
nVolCol=round(nVolCol)
for q=1:nVolCol
strrep(files(q).name, '_', ' ');
str = sprintf('Umax %s',files(q).name);
new_str=strrep(files(q).name, '_', ' ');
new_str2=strrep(new_str, '.xlsx', ' '); new_str3=strrep(new_str2, 'RA 20.5V 2US', ' ');
subplot(nVolCol/2,nVolCol,[q+q-1, q+q])
counts = hist(loopDataVol(:,q),centers);
bar(counts / sum(counts));
set(gca,'FontSize',10,'FontWeight','Bold')
set(0,'DefaultAxesLineStyleOrder',{'-','--',':'}) %or whatever order you want
% title('Velocity Distribution Row1')
title([' VolDist ',new_str3]);
t=annotation('textbox', [0.28, 0.885, 0.1, 0.1], 'string', ' VOLUME DISTRIBUTIONS')
% sz = t.FontSize;
% t.FontSize = 13;
% t.FontWeight = 'bold';
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!