Displaying Text on Paper
7 次查看(过去 30 天)
显示 更早的评论
I'm printing out something like 400 sheets of paper through MATLAB, and I'd like to be able to display the slice number in the corner of the paper. I've attached a preview of what one of the slices will look like. The thing is, the figure doesn't cover the entire paper; the figure's only as big as the brain in the center of the paper, so it's hard to insert text outside the figure. Does anyone have any ideas as to how to put text in the bottom right corner of the paper?
0 个评论
回答(2 个)
Star Strider
2024-5-29
编辑:Star Strider
2024-5-29
Without knowing more about this, one option would be to use the axis function to extend the axis limits. Then the text function should be able to provide the required information at the chosen (x,y) location.
EDIT — (29 May 2024 at 19:19)
Illustrating this approach —
BrainSlice = imread('Brain Slice — Screenshot 2024-05-29 at 12-52-04 image.png (PNG Image 1355 × 949 pixels) — Scaled (80%).png');
figure
image(BrainSlice)
figure
image(BrainSlice)
ylim([0 250])
slice_nr = 142;
text(max(xlim)-65, max(ylim)-10, "Slice # "+slice_nr, 'FontWeight','bold' )
figure
image(BrainSlice)
ylim([0 250])
slice_nr = 142;
text(max(xlim)-65, max(ylim)-10, "Slice # "+slice_nr, 'FontWeight','bold' )
Ax = gca;
Ax.Visible = 0;
I took a screenshot of your image to use in this example. You will likely need to tweak the (x,y) coordinates in the text call to work with your image, although I did my best to make it adaptive.
You can also use sprintf to create the text label, for example:
text(max(xlim)-65, max(ylim)-10, sprintf('Slice # %3d', slice_nr), 'FontWeight','bold' )
I defer to you for those details.
.
5 个评论
Star Strider
2024-5-30
I cannot determine what you want, and I do not have access to your computer to experiment with it. (I also so not know what app you are using to create the screencap you posted.)
Experiment with the (x,y) coordinates (the first two arguments of the text function) to get the result you want. You should be able to place it anywhere within reason.
Perhaps —
BrainSlice = imread('Brain Slice — Screenshot 2024-05-29 at 12-52-04 image.png (PNG Image 1355 × 949 pixels) — Scaled (80%).png');
figure
imagesc(BrainSlice)
title('Original')
figure
imagesc(BrainSlice)
axis('equal')
slices = 250;
i = 142;
text(max(xlim)-100, max(ylim)+80, sprintf('SLICE %d OF %d\n', i, slices), 'FontWeight','bold' )
ylim([min(ylim) max(ylim+90)])
figure
imagesc(BrainSlice)
axis('equal')
slices = 250;
i = 142;
text(max(xlim)-100, max(ylim)+80, sprintf('SLICE %d OF %d\n', i, slices), 'FontWeight','bold' )
ylim([min(ylim) max(ylim+90)])
Ax = gca;
Ax.Visible = 0;
This is the best I can do.
.
Voss
2024-5-30
编辑:Voss
2024-5-30
You will need to change PaperPosition property of the figure (or the PaperSize property, depending on the output image format you want), and decrease the size of the axes relative to the figure, in order to have the figure span the whole page (so you can put a text in the lower-right corner) while keeping the axes where it is now relative to the page.
Here's an example:
f = figure();
ax = gca();
im = image(ax);
ax.Position = [0.4 0.4 0.2 0.2];
t = text( ...
'Parent',ax, ...
'Units','normalized', ...
'Position',[2.75 -1.85], ...
'String','Page 1');
f.PaperUnits = 'centimeters';
f.PaperType = 'usletter';
f.PaperPosition = [0 0 27.94 21.59];
f.PaperOrientation = 'landscape';
When I "Print Preview..." on that figure, I get the following:
And the saved pdf (attached) has the page number string in the lower-right corner of the page, as intended.
Obviously you'll need to make adjustments to get the exact result you want. In particular, you'll probably want to make the axes bigger than I've done here, which will require adjusting the text position (since it is normalized to the axes), but hopefully this proof-of-concept is sufficient to get you started down the right path.
3 个评论
Voss
2024-5-31
You're welcome!
"Unfortunately the paper position property of the figure is fixed."
In order to enforce a 1mm by 1mm pixel size, you should be fixing the image size. Since the image spans the axes, you can fix the axes size as well to achieve the same effect. Fixing the figure size or PaperPosition doesn't achieve the desired pixel size because of the extra space in the figure around the axes.
With the axes size fixed, the PaperPosition can be big enough to have the page number text in the bottom corner of the page. I find it convenient for the PaperPosition to span the entire page.
Here's an example. It's the same approach as in my answer, but with the additional constraint that the axes size is such that the image pixels are 1mm by 1mm.
ind = randi(255,100,150); % random 100x150 image
[dim1,dim2] = size(ind);
bfig = figure;
imagesc(ind)
colormap(flipud(gray))
axis off;
px = 21.59;
py = 27.94;
bfig.PaperOrientation = 'landscape';
bfig.PaperUnits = 'centimeters';
bfig.PaperPosition = [0 0 py px];
bfig.Units = 'centimeters';
bfig.Position = [0 0 py px];
ax_pos = [(py - dim2/10)/2 ,(px - dim1/10)/2, dim2/10, dim1/10];
set(gca(),'Units','centimeters','Position',ax_pos);
text( ...
'Units','centimeters', ...
'Position',[py-ax_pos(1) -ax_pos(2)], ...
'HorizontalAlignment','right', ...
'VerticalAlignment','bottom', ...
'String', 'Slice 265 of 400', ...
'FontSize', 22)
When I "Print Preview..." on that figure, I get the following:
And the saved pdf (attached) has the page number string in the lower-right corner of the page, as intended.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Annotations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!