Export a scaled graph to pdf for printing

2 次查看(过去 30 天)
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');

采纳的回答

Anudeep Kumar
Anudeep Kumar 2025-4-21
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 个评论
Luke Cabot
Luke Cabot 2025-4-21
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!

请先登录,再进行评论。

更多回答(0 个)

标签

Community Treasure Hunt

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

Start Hunting!

Translated by