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

7 vues (au cours des 30 derniers jours)
gsourop
gsourop le 15 Déc 2017
Modifié(e) : OCDER le 15 Déc 2017
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 commentaires
KALYAN ACHARJYA
KALYAN ACHARJYA le 15 Déc 2017
Sorry, I am not getting your question.
gsourop
gsourop le 15 Déc 2017
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.

Connectez-vous pour commenter.

Réponse acceptée

OCDER
OCDER le 15 Déc 2017
Modifié(e) : OCDER le 15 Déc 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

Plus de réponses (0)

Catégories

En savoir plus sur Specifying Target for Graphics Output dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by