HCAI
HCAI

Reputation: 2263

Getting screen units matlab for saving to pdf

I would like to export this picture to pdf format:

enter image description here

P2_tilde =

0.1029    0.4118    0.0245    0.1814    0.2794
0.3925    0.0234    0.0280    0.4626    0.0935
0.0928    0.1237    0.2680    0.2990    0.2165
0.0699    0.2219    0.0182    0.5106    0.1793
0.2611    0.0887    0.0837    0.3251    0.2414

figure('color',[1,1,1])

hBar2=bar3(P2_tilde);
colormap('pink')

set(hBar2,{'CData'},C);   
set(gca,'xticklabel',surfaces)
set(gca,'yticklabel',surfaces)
surfaces={'Equipment','Patient','Hygiene products','Near-bed','Far-bed'};
colorbar
zlabel('Probability');

colormap('pink')
colorbar('location','southOutside')
set(gca,'xticklabel',surfaces)
set(gca,'yticklabel',surfaces)
surfaces={'Equipment','Patient','Hygiene products','Near-bed','Far-bed'};
zlabel('Probability');

Want to export to PDF format:

currentScreenUnits=get(gcf,'Units')     % Get current screen units
currentPaperUnits=get(gcf,'PaperUnits') % Get current paper units
set(gcf,'Units',currentPaperUnits)      % Set screen units to paper units
plotPosition=get(gcf,'Position')        % Get the figure position and size
set(gcf,'PaperSize',plotPosition(3:4))  % Set the paper size to the figure size
set(gcf,'Units',currentScreenUnits)     % Restore the screen units

print -dpdf ptilde      % PDF called "ptilde.pdf"

Gives something completely off the page. Any thoughts how to center the figure on the canvas and make it only the size of the figure? Otherwise how can I trim it?

Upvotes: 2

Views: 201

Answers (2)

Molly
Molly

Reputation: 13610

The PaperSizer parameter needs to be the actual size of the paper it will be printed on (the pdf file that will be displayed) not the size that the Matlab figure appears on the screen. For example, if you change it to this:

set(gcf,'PaperSize',[9,11])  

you'll get something that looks reasonable.

Upvotes: 2

Dominik
Dominik

Reputation: 792

I couldn't quite replicate your figure (errors in your code: need a definition for C) but use

f=figure('color',[1,1,1]);
%rest of figure code....
set(f,'PaperPositionMode','auto')
print -dpdf ptilde

Also, I'm not sure how to crop the pdf from within MATLAB, but if you want a cropped vector graphic use -depsc or -depsc2 as the print flag. see MATLAB help on print.

Upvotes: 2

Related Questions