我想在信纸上打印一张图像,宽度为 25.4 厘米,高度为 21.2 厘米(二维数组是 212 x 254 个元素,所以我希望每个元素占用一个打印纸上毫米的空间)。我在打印之前使用 printpreview 功能查看图像,“布局”和“纸张”部分似乎具有我正在寻找的规格,但图像似乎不是我想要的尺寸它是(图像应该占据纸张的~90%,但事实并非如此)。夸张地说,我将图形尺寸更改为大于纸张本身的尺寸,但软件似乎处理得很好。我应该怎么做才能使图像符合我指定的尺寸?
这是我的代码:
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`
我也尝试过对图形的大小进行硬编码,但无济于事。
尝试以下代码,
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
您可以修改
ax.Position
边距以获得您想要的结果。