getframe captures only a corner of the axes

조회 수: 9 (최근 30일)
AR
AR 2025년 8월 5일
댓글: Walter Roberson 2025년 8월 6일
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