Export a scaled graph to pdf for printing

2 views (last 30 days)
Hi all! very new to MATLAB here.
I have a code that plots a template for cutting exhaust pipe at user defined angles. I want to print out the curve, to scale, so I can cut it out and draw around pipe with it. For reference, the usual exhaust diameter is 63mm and cut angle 7 degrees.
The code works fine and gives the x and y axis in the correct units. However, from line 33 I am struggling. I have tried a few things, but the pdf will not print in cm, to scale. Any thoughts are greatly appreciated:
% pie cut template v1.2
% exports a printable template to pdf
clear all
close all
clc
d_pipe = input('Enter pipe outer diameter, (mm) ');
disp(d_pipe);
c_pipe = pi*d_pipe;
disp('pipe circumference (mm) ');
disp(c_pipe);
c_pipe_cm = c_pipe/10;
angle_cut = input('enter cutting angle (deg) ');
disp(angle_cut);
angle_cut_rad = angle_cut*pi/180;
amp_cm = d_pipe*tan(angle_cut_rad)/20;
x = linspace(-pi, pi, 1000);
x_cm = linspace(0,c_pipe_cm,1000);
y = amp_cm*cos(x)+amp_cm;
figure;
plot(x_cm, y, 'k', 'Linewidth', 3);
xlabel('Pipe circumference (cm)');
ylabel('Cutting template (cm)');
% title('Cutting template');
% graph limits for template
xlim([0, c_pipe_cm]);
% set figure size and position
width_cm = c_pipe_cm;
height_cm = amp_cm;
set(gcf, 'units', 'centimeters', 'position', [0, 0, width_cm, 5]);
% ensure pdf matches figure size
set(gcf, 'paperunits', 'centimeters');
set(gcf, 'papersize', [width_cm, height_cm]);
set(gcf, 'paperposition', [0, 0, width_cm, height_cm])
% export to pdf
print(gcf, 'Pipe template.pdf', '-dpdf', '-r300');

Accepted Answer

Anudeep Kumar
Anudeep Kumar on 21 Apr 2025
Hey Luke,
I tried out the code you shared and made a few adjustments that should help ensure your PDF exports exactly to scale.
clear all
close all
clc
d_pipe = input('Enter pipe outer diameter, (mm) ');
disp(d_pipe);
c_pipe = pi*d_pipe;
disp('pipe circumference (mm) ');
disp(c_pipe);
c_pipe_cm = c_pipe/10;
angle_cut = input('enter cutting angle (deg) ');
disp(angle_cut);
angle_cut_rad = angle_cut*pi/180;
amp_cm = d_pipe*tan(angle_cut_rad)/20;
x = linspace(-pi, pi, 1000);
x_cm = linspace(0,c_pipe_cm,1000);
y = amp_cm*cos(x)+amp_cm;
figure;
plot(x_cm, y, 'k', 'Linewidth', 3);
xlabel('Pipe circumference (cm)');
ylabel('Cutting template (cm)');
% title('Cutting template');
% graph limits for template
xlim([0, c_pipe_cm]);
% set figure size and position
width_cm = c_pipe_cm;
height_cm = 2*amp_cm;% double amplitude for enough vertical space
set(gcf, 'units', 'centimeters', 'position', [0, 0, width_cm, height_cm]);
% Added line
set(gca, 'Position', [0 0 1 1], 'Units', 'normalized');
% ensure pdf matches figure size
set(gcf, 'paperunits', 'centimeters');
set(gcf, 'papersize', [width_cm, height_cm]);
set(gcf, 'paperposition', [0, 0, width_cm, height_cm])
% export to pdf
print(gcf, 'Pipe template.pdf', '-dpdf', '-r300');
Changes I made:
  • On line 34, I set height_cmto twice the amplitude to give you more vertical space.
  • On line 36, I updated the figure position to use the new “height_cm” value.
  • I also added a line to set the axes position (set(gca, 'Position', [0 0 1 1], 'Units', 'normalized');). This ensures the exported PDF is to scale, fills the entire page, and doesn't have unwanted margins or whitespace.
When printing, just make sure to select “100%” or “Actual size” in your print settings. This should give you a template that matches your specified dimensions exactly.
Here is the documentation on various properties for Axes, for your reference:
Hope this helps you get the desired result!
  1 Comment
Luke Cabot
Luke Cabot on 21 Apr 2025
Hi Anudeep
That's perfect, thankyou!
I tried twice the height before and some other gca settings before, but could not quite get it to work. Your ammendments are exactly to scale. I will try moving the template on the printable area but everything else is fine. Cheers!

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!