- Undocumented Matlab - https://undocumentedmatlab.com -
Customizing print setup
Posted By Yair Altman On November 25, 2009 | 9 Comments
Last week, while consulting a client, I watched him use a Matlab application that processed data and presented the results in a figure window. I saw that he constantly needed to go to File/Print-Preview menu option to customize the figure’s print setup before being able to print. He would constantly click the “Fill Page” button, then go to the Color tab and set RGB rather than Black-and-White, and finally go to the Advanced tab to prevent printing UI controls. Only then would he actually print the figure using the menu (File/Print) or the toolbar Print button.
Of course, lack of a documented method never stopped us before. Sure enough, after a short search I discovered the hidden potential of the undocumented/unsupported setprinttemplate function. This function, called internally by Matlab’s print-related functions, is responsible for setting the figure’s initial print setup. Once I had this key, unlocking the problem was easy. Here is the bottom line:
The figure’s default print setup is stored in the figure’s hidden ApplicationData property, accessible via the getappdata and setappdata functions or directly via get/set, as explained in a previous post [2]. More importantly, the figure-specific setup is stored in another hidden property, PrintTemplate. Both of these setup data are stored in structure format, which is not available when a figure is first created, but only after printing or print-preview. Note that printtemplate.m contains detailed explanations about the meaning of some fields – to see them, simply display the file:
>> type printtemplate.m % or: edit printtemplate.m
Unless we set PrintTemplate ourselves (or call print or printpreview to do this), printing the figure will use the default print setup. To set a non-standard setup, we just need to create the PrintTemplate structure with our non-default setup options. Matlab will then automatically use them when printing the figure. Here is a checklist for doing so:
% Create a simple figure
>> hFig = figure; surf(peaks);
% Open the print-preview window to create setup data
>> printpreview(hFig);
% Retrieve the original (default) print setup data
>> oldSetup = get(hFig,'PrintTemplate')
oldSetup =
VersionNumber: 2
Name: ''
FrameName: ''
DriverColor: 1
AxesFreezeTicks: 0
tickState: {}
AxesFreezeLimits: 0
limState: {}
Loose: 0
CMYK: 0
Append: 0
Adobecset: 0
PrintUI: 1
Renderer: 'auto'
ResolutionMode: 'auto'
DPI: 0
FileName: 'untitled'
Destination: 'printer'
PrintDriver: ''
DebugMode: 0
StyleSheet: 'default'
FontName: ''
FontSize: 0
FontSizeType: 'screen'
FontAngle: ''
FontWeight: ''
FontColor: ''
LineWidth: 0
LineWidthType: 'screen'
LineMinWidth: 0
LineStyle: ''
LineColor: ''
PrintActiveX: 0
GrayScale: 0
BkColor: 'white'
FigSize: [14.8054083333333 11.10405625]
>> newSetup = get(hFig,'PrintTemplate');
>> objdiff(oldSetup,newSetup)
ans =
DriverColor: {[0] [1]}
FigSize: {[14.8054083333333 11.10405625] [2x1 double]}
PrintUI: {[1] [0]}
StyleSheet: {'default' 'modified'}
limState: {{} ''}
tickState: {{} ''}
function MyFig_OpeningFcn(hObject, eventdata, handles, varargin)
...
% This was adapted from initprintexporttemplate.m
pt = printtemplate;
pt.StyleSheet = 'modified';
pt.VersionNumber = 2; % important (Note #1 below)
pt.FigSize = [38.1, 21.0];
pt.DriverColor = 1;
pt.PrintUI = 0;
% we must set the paper size *before* setprinttemplate
set(hObject, 'PaperPositionMode','manual', ...
'PaperPosition',[0 0.5 29.5 20], ...
'PaperSize',[29.68 20.98], ...
'PaperType','A4');
setprinttemplate(hObject, pt);
% Choose default command line output for MyFig
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
That’s all there is to it. So easy once we know how, isn’t it? The most annoying pain-in-the-so-and-so sometimes have simple solutions…
Note #1: it is very important to set pt.VersionNumber to 2, otherwise some modifications will not take effect.
Note #2: the internal implementation of printtemplate as well as the internal setup fields have changed between Matlab releases. These were often minor backward-compatible changes, but at least once this was a major change (VersionNumber 1=>2, I think around Matlab 7.2, but I’m not sure). Therefore, carefully test your code on the oldest release which is supposed to run it. Also, if you plan the code to run in future Matlab releases, you should note that the entire setup functionality might break without prior notice, since it is an internal unsupported implementation.
Categories: Figure window, Hidden property, Medium risk of breaking in future versions, Semi-documented function, Stock Matlab function, Undocumented feature
Article printed from Undocumented Matlab: https://undocumentedmatlab.com
URL to article: https://undocumentedmatlab.com/articles/customizing-print-setup
URLs in this post:
[1] Image: http://undocumentedmatlab.com/images/PrintPreview.png
[2] explained in a previous post: http://undocumentedmatlab.com/blog/displaying-hidden-handle-properties/
[3] objdiff utility: http://www.mathworks.com/matlabcentral/fileexchange/14395-objdiff-generic-object-comparator
[4] Capturing print events : https://undocumentedmatlab.com/articles/capturing-print-events
[5] Customizing uitree : https://undocumentedmatlab.com/articles/customizing-uitree
[6] Customizing uiundo : https://undocumentedmatlab.com/articles/customizing-uiundo
[7] Customizing axes part 3 – Backdrop : https://undocumentedmatlab.com/articles/customizing-axes-part-3-backdrop
[8] Customizing axes part 4 – additional properties : https://undocumentedmatlab.com/articles/customizing-axes-part-4-additional-properties
[9] Customizing axes rulers : https://undocumentedmatlab.com/articles/customizing-axes-rulers
[10] : http://www.mathworks.com/matlabcentral/fileexchange/26400-function-readyforprint
[11] : http://itb.biologie.hu-berlin.de/~schaette/HomeFiles/MatlabPlots.pdf
Click here to print.
Copyright © Yair Altman - Undocumented Matlab. All rights reserved.
9 Comments To "Customizing print setup"
#1 Comment By Sebastiaan On January 5, 2010 @ 05:51
Is this not what the ‘StyleSheet’ option in the print dialog is used for?
#2 Comment By Yair Altman On January 5, 2010 @ 06:34
Sebastiaan – the point in this post was to enable full programmatic access to the Print options customization, without need for any manual intervention.
#3 Comment By Andy On March 16, 2010 @ 09:16
I’ve struggled with this for years. I have some preferred styles for journal papers, others for web pages and yet more for presentations. I gave up and wrote a relatively simple function that addresses much of the above.
I don’t want to repost the details here, but see matlabcentral for my solution – a ‘readyforprint’ function;
[10]
the result is that a single command can be used to produce nicely-formatted images;
produces images 5.08 inches wide, 2.21 tall, 8 pt text, black foreground (lines and markers) and white background. All the lines are 1 point thick. Gives very nice output for a journal figure!
#4 Comment By Daniel On July 28, 2010 @ 11:17
The OP didn’t work well. Another search found the following.
[11]
Code to “Fill page” for US letter.
#5 Comment By Rich On June 23, 2011 @ 11:30
New to Matlab and this appears to be the solution I am after. However, I stumble at step 4 at “or in your figure’s initialization function (for non-GUIDE figures)” I cant seem to make this work. Could you possibly provide a code snippet or clues as to how to implement for a “non-GUIDE” figure. Thanks
#6 Comment By Yair Altman On June 23, 2011 @ 11:54
Step 4 already contains a code snippet. You just need to adapt it in your code (hObject is the figure handle). If you want my personal assistance, please contact me by email.
#7 Comment By Jim Kay On August 31, 2016 @ 11:16
I agree with Rich – this is not very clear for non-Guide things.
Ultimately I am happy using a ‘mydefault’ stylesheet. I just want to know how to get Matlab to load ‘mydefault’ automatically at startup.
But anyway thanks for your hard work running this website.
Jim
#8 Comment By akash garg On September 7, 2012 @ 01:04
Sir i want to print barcode from matlab. i generated barcode using text command but it cannot be readable by barcode scanner. am using godex ez2100 printer for printing.
please help me if possible
#9 Comment By Yair Altman On September 7, 2012 @ 05:35
@Akash – try using a different barcode font face, or increase your font size