Clear Filters
Clear Filters

How to print multiple figures to same PDF page?

52 views (last 30 days)
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 Comment
Jan
Jan on 6 Oct 2017
Edited: Jan on 6 Oct 2017
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.

Sign in to comment.

Answers (3)

Sean de Wolski
Sean de Wolski on 6 Oct 2017
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 on 6 Oct 2017
Edited: Jan on 6 Oct 2017
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 Comments
DrWooo
DrWooo on 6 Oct 2017
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 on 9 Oct 2017
Edited: Jan on 9 Oct 2017
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:

Sign in to comment.


Siranjeevi Gurumani
Siranjeevi Gurumani on 11 Oct 2020
Write your code in live script and export the results to pdf.

Categories

Find more on Historical Contests 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!