Displaying Text on Paper

7 次查看(过去 30 天)
Isabella
Isabella 2024-5-29
评论: Isabella 2024-5-31
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?

回答(2 个)

Star Strider
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 个评论
Isabella
Isabella 2024-5-30
编辑:Isabella 2024-5-30
Hello again,
The reason why the text you've placed is still visible is because even though the text is outside the boundaries of the axes, it's still within the boundaries of the figure. Text placement is convenient enough if it occurs within the boundaries of the figure, but my issue is that I'd like to place text outside the boundaries of the figure.
(Axes only span the blue portion of the figure, but the entire figure is enclosed by the red portion).
Compare that to this, in which I've attempted to outline what I think are the boundaries of the figure on the paper:
And I need the text to be all the way in the bottom right corner of the paper. Even if I adjust the parameters of the code you've given me, the text is cut as soon as it leaves the boundaries of the figure.
Star Strider
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
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
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.
Isabella
Isabella 2024-5-31
Hey Voss, I've imported your code and messed around with it a little. The extra space around the figure has been accounted for, I assure you (I agonized over this for quite a while). To remove the invisible space take up by the labels and such, I use this line of code:
set(gca, 'Position', [0 0 1 1]);
Thus, when I print it out on paper, each pixel does measure 1mm by 1mm (I've measured it. You wouldn't believe how much paper I've wasted on this project so far).
While your approach does let me put text in the corner of the screen, it warps the figure a little. See here:
^ Prints 1mm x 1mm.
^ The code you sent me. As you can see, the photo is slightly warped.
Honestly, this seems to be giving both MATLAB Answers and Stack Overflow a hard time, mainly because all the answers I've been given warp or edit the figure in some way, when I'd really like to leave the figure untouched since its size and position has to be so carefully defined. I think I'll just stick to having the text in the corner of the figure instead, to be honest. It looks quite nice there and it won't give me, or any of you, a hard time. Thank you kindly for your help thus far.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Annotations 的更多信息

标签

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by