Search code examples
matlabprinting

MATLAB: Trouble Using printpreview


I've got an image I'd like to print on letter paper with a width of 25.4 cm, and a height of 21.2 cm (the 2D array is 212 x 254 elements, so I'd like each element to take up a millimeter of space on printed paper). I'm using the printpreview function to take a look at the image before I print it, and the Layout and Paper sections seem to have the specifications I'm looking for, but the image does not appear to be the size I'd like it to be (the image should take up ~90% of the paper, but it doesn't). To overexaggerate, I changed the figure dimensions to be something larger than the size of the paper itself, but the software seems to be handling it just fine. What should I do so that the image fits the dimensions I specify?

Here's my code:

clear
clc
close all

x = 27.94;
y = 21.59;
xmarg = (27.94 - 25.4)/2;  
ymarg = (21.59 - 21.2)/2;
xsize = 25.4;
ysize = 21.2;

metaData = niftiinfo('index_image.nii');
vol = niftiread(metaData);

[volProc] = preProcessBrainCANDIData(vol, metaData, true);

[saggital, coronal, transverse] = size(volProc);
slice = volProc(:, :, transverse / 2);

sliceViewer(vol)

bfig = figure;
imagesc(slice)
axis off;
colormap(gray) 

clim([0, 255]);

set(bfig, 'Units','centimeters', 'Position',[0 0 xsize ysize])
movegui(bfig, 'center')

set(bfig, 'PaperUnits', 'centimeters');
set(bfig, 'PaperPosition',[xmarg ymarg xsize ysize])
set(bfig, 'PaperOrientation', 'landscape');

printpreview`

I've also just tried hard coding in the size of the figure, to no avail.


Solution

  • Try the following code,

    clear
    clc
    close all
    
    x = 27.94;
    y = 21.59;
    xmarg = (27.94 - 25.4)/2;  
    ymarg = (21.59 - 21.2)/2;
    xsize = 25.4;
    ysize = 21.2;
    
    % random slice per your suggestion!
    
    slice = randi(255, 212,254);
    
    % set boundaries to 200 to make them visible
    slice(:, 1:5)=200;
    slice(:, end-5:end)=200;    
    slice(1:5,:)=200;
    slice(end-5:end, :)=200;
    
    bfig = figure;
    imagesc(slice)
    axis off;
    colormap(gray) 
    
    clim([0, 255]);
    
    set(bfig, 'Units','centimeters', 'Position',[0 0 xsize ysize])
    movegui(bfig, 'center')
    
    set(bfig, 'PaperUnits', 'centimeters');
    set(bfig, 'PaperPosition',[xmarg ymarg xsize ysize])
    set(bfig, 'PaperOrientation', 'landscape');
    
    % Added material
    ax = gca;
    outerpos = ax.OuterPosition;
    ti = ax.TightInset; 
    left = outerpos(1) + ti(1);
    bottom = outerpos(2) + ti(2);
    ax_width = outerpos(3) - ti(1) - ti(3);
    ax_height = outerpos(4) - ti(2) - ti(4);
    ax.Position = [left bottom ax_width ax_height];
    
    
    printpreview
    

    This gives me the following result, print without margins

    you can modify ax.Position margins to get your desired result.