How to generate multiple graphs per page in several pages in one loop?
7 views (last 30 days)
Show older comments
Hi everyone,
I would like to create a single loop that will generate several pages of graphs. Moreover, I would like each page to contain 8 graphs. For example, if I have a matrix
A = randn(100,16);
I would like to create 2 pages of 8 graphs each. How could I change the following code in order to create the graphs in one loop
varnames_i = {'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16'};
time = datetime(2000,1:100,1);
figure;
for k = 1:8
subplot (4,2,k)
filename1 = plot( time' , A( : , k ) );
xlim(datenum([min(time) max(time)]))
title ( varnames_i(k));
hold on
end
saveas(filename1, '1_8.jpg');
figure;
for k = 1:8
subplot (4,2,k)
filename2 = plot( time' , A( : , 8+k ) );
xlim(datenum([min(time) max(time)]))
title ( varnames_i(8+k));
hold on
end
saveas(filename2, '9_16.jpg');
4 Comments
Accepted Answer
OCDER
on 15 Dec 2017
Edited: OCDER
on 15 Dec 2017
N = 8;
A = rand(100, N); %N is an integer number > 0
% I don't think you need this:
% varnames_i = {'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16'};
%
% But here's a way to make this algorithmically
% varnames_i = cellfun(@(x) sprintf('%d', x), num2cell(1:size(A,2)), 'unif', false);
time = datetime(2000,1:100,1);
for v = 1:size(A, 2)
varname = sprintf('%d', v);
figure;
for k = 1:8
if size(A, 2) < k; break; end %Added this in case your size(A,2) < 8, which errors out
%filename1 = plot(time', A(:, k)); %confusing to name a plot handle
% as a "filename1". Plus, you need the figure handle (gcf) instead
% of plot handle when using saveas
%xlim(datenum([min(time) max(time)])) no need for datenum
subplot(4, 2, k)
plot(time', A(:, k));
xlim([min(time) max(time)])
title(varname);
hold on
end
filename = sprintf('%d_%d.jpg', 1+(v-1)*8, v*8); %makes the name 1_8.jpg, 9_16.jpg, ...
saveas(gcf, filename);
end
0 Comments
More Answers (0)
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!