필터 지우기
필터 지우기

How to print multiple figures to same PDF page?

조회 수: 56 (최근 30일)
DrWooo
DrWooo 2017년 10월 5일
답변: Siranjeevi Gurumani 2020년 10월 11일
Hi. I have a set of 6 figures I would like to print to 3 PDF pages (2 figures per page). I looked around at previous questions and found a workaround to printing figures into a PDF that involves first printing the figures as a ps and then converting to a PDF. Once it is a PDF, I could manually combine the figures to the same page, but I am hoping there is a more efficient way to tell Matlab to do this when printing is as a ps file.
Currently, I have two sets of code -- the first is a set of 6 independent nested for loops (one for each figure). I have successfully printed these figures to a single ps file and then converted it to a PDF, but each figure is on it's own page. Below is the nested for loop for the 5th figure:
for x = B(44:47,5) %B is the excel file the dataset is pulled from
for y = B(44:47,8);
figure(5);
scatter(x,y)
hold on
lsline
mdl = fitlm(x,y)
title('Chart Title');
xlabel('x-axis');
ylabel('y-axis');
str = 'linear regression data';
text(10,16,str);
print('-dpsc2','FileName','-append','-f5');
end
end
I ran each of the above individual nested for loops once to find the linear regression data, then went back and added that in as a string so that it would appear on the figure (I am unsure if it is possible to have Matlab do this for me). The second code below is a single nested for loop that more efficiently prints the same figures as the above individual codes; however, I am unsure how to run the mdl = fitlm(x,y) line within this loop. I am also unsure how to print the figures in this loop to a ps file.
u = unique(B(:,1));
n = {'Title Fig1'; 'Title Fig2'; 'Title Fig3'; 'Title Fig4'; 'Title Fig5'; 'Title Fig6'};
r = {'linear reg data Fig1'; 'lrd Fig2'; 'lrd Fig3'; 'Lrd Fig4'; 'lrg Fig5'; 'lrg Fig6'};
for k=1:numel(u)
d = B(B(:,1)==u(k),:);
figure; scatter(d(:,5),d(:,8)); grid on;
title(n{k});
xlabel('x-axis');
ylabel('y-axis');
dim = [.2 .5 .3 .5];
annotation('textbox',dim,'String',r{k},'FitBoxToText','on');
lsline
end
Any suggestions for how to address this are greatly appreciated.
Cheers
  댓글 수: 1
Jan
Jan 2017년 10월 6일
편집: Jan 2017년 10월 6일
I do not understand this sentence:
I ran each of the above individual nested for loops once to find the
linear regression data, then went back and added that in as a string so
that it would appear on the figure
What does "more efficiently" mean, when the expensive part "fitlm(x,y)" is omitted?
I am also unsure how to print the figures in this loop to a ps file.
Exactly like in the first code?!
It is not clear, what the actual question is. Getting a useful answer is more likely, if you concentrate on one problem per thread. For printing, it does not matter how you create the diagrams. If the goal is to make the code "more efficient", the printing to EPS is not interesting. So prefer to focus on one problem after the other.
The tags are used to search related threads. "Excel" or "linear regression" does not hit the point.

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

답변 (3개)

Sean de Wolski
Sean de Wolski 2017년 10월 6일
This is easily doable with MATLAB Report Generator though it could be overkill.
Example requires 17b but could be done using other things in <=17a.
import mlreportgen.report.*
import mlreportgen.dom.*
report = Report('peaks','pdf');
for ii = 1:3
surf(peaks(20));
figure = Figure();
peaks20 = Image(getSnapshotImage(figure,report));
peaks20.Width = '3in';
peaks20.Height = [];
add(report,peaks20);
surf(peaks(40));
figure = Figure();
peaks40 = Image(getSnapshotImage(figure,report));
peaks40.Width = '3in';
peaks40.Height = [];
delete(gcf);
add(report,peaks40);
add(report,PageBreak);
end
close(report);
rptview(report);

Jan
Jan 2017년 10월 6일
편집: Jan 2017년 10월 6일
Instead of printing 2 figures on one PDF page, it would be much easier to copy the objects of two figures to one figure and export it to a PDF in the standard way.
  댓글 수: 2
DrWooo
DrWooo 2017년 10월 6일
Hi Jan,
Do you mean via subplot? I tried doing that, but didn't like how small both of the plots looked (since the figure size was still the same). I think I'll stick with printing it as a ps file and converting to a pdf, since everything I've tried takes an additional step anyways.
In case someone else finds this useful (in regards to the mdl = fitlm), I reworked my code so it looks like:
for k=1:numel(u)
d = B(B(:,1)==u(k),:);
figure; scatter(d(:,7),d(:,8)); grid on;
title(n{k});
xlabel('x-axis');
ylabel('y-axis');
lsline
mdl = fitlm(d(:,7),d(:,8));
textstr = {num2str(mdl.Rsquared.Ordinary)};
dim = [.2 .5 .7 .5];
annotation('textbox',dim,'String',['r^2 =' textstr],'FitBoxToText','on');
print('-dpsc2','filename','-append');
lsline
end
Jan
Jan 2017년 10월 9일
편집: Jan 2017년 10월 9일
Yes, I meant a kind of subplot. Instead of the subplot command, you can copy the contents of the existing figures to axes, which are created with the wanted dimensions directly.
Search in the FileExchange for "subplot": https://www.mathworks.com/matlabcentral/fileexchange/?utf8=%E2%9C%93&term=subplot . E.g. this might be useful:

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


Siranjeevi Gurumani
Siranjeevi Gurumani 2020년 10월 11일
Write your code in live script and export the results to pdf.

카테고리

Help CenterFile Exchange에서 Historical Contests에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by