getframe captures only a corner of the axes

13 次查看(过去 30 天)
I am running the following code to try to capture an axis, as shown in the leftmost attached image.
figure;
plot(1:10, 1:10);
frame = getframe;
figure;
imagesc(frame.cdata)
The output of the above code is in the second attached image. As you can see, it results in an inexplicably cropped version of the axes, where only the bottom left corner of the axes is captured. I tried playing with the figure and axis position, as well as different settings of the axis ("tight," "off," etc.) but to no avail. The same thing happens with getframe(gca), so specifying the axis does not help. I also tried playing with getframe(gca, Rect) -- for example, I do getframe(gca, getpixelposition(gca)), but I got an error that the rectangle doesn't fit within the figure.
If I use getframe(gcf), that doesn't happen, but capturing the whole figure adds some extra whitespace even when I do "axis off", as can be seen in the last attached image, where I have plotted the resulting frame.cdata with imagesc. I don't want that deadspace. I want to capture the axis exactly as it is, no more, no less. In addition, I don't want to use exportgraphics or anything else. I want the underlying pixel values as an array.
Finally, I tried using the print function, but that didn't work either. It has the same sort of issues as explained in the previous paragraph with adding whitespace to the captured figure.

采纳的回答

AR
AR 2025-8-6
Resolved (I think)!
I found the following code to eliminate the (asymmetrical) gray padding on the margin of the figure, which encircles the axis. Although getframe / getframe(gca) still behaves bizarrely, and I don't have an answer to why that's happening, I can now use this code and then do getframe(gcf).
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];

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

标签

产品


版本

R2025a

Community Treasure Hunt

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

Start Hunting!

Translated by