How to generate multiple graphs per page in several pages in one loop?

조회 수: 9 (최근 30일)
gsourop
gsourop 2017년 12월 15일
편집: OCDER 2017년 12월 15일
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
KALYAN ACHARJYA
KALYAN ACHARJYA 2017년 12월 15일
Sorry, I am not getting your question.
gsourop
gsourop 2017년 12월 15일
I am saying that if A is a matrix 100x16 I could just use the code I wrote above. But if the dimensions of A increase, for example to 100x80, I should rewrite the code above 8 times. Such as,
varnames_i = {'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24'};
% 1st time
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');
% 2nd time
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');
% 3rd time
figure;
for k = 1:8
subplot (4,2,k)
filename2 = plot( time' , A( : , 16+k ) );
xlim(datenum([min(time) max(time)]))
title ( varnames_i(16+k));
hold on
end
saveas(filename2, '17_24.jpg');
% etc
So I would like to create a loop that does this iteration, instead of doing it manually.

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

채택된 답변

OCDER
OCDER 2017년 12월 15일
편집: OCDER 2017년 12월 15일
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개)

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by