我知道关于这个问题有很多答案,但我没有找到任何对我有帮助的答案。 我在 MATLAB 中有一个带有 2 个轴的 GUI,我想将每个轴单独保存为 .jpeg 或任何其他格式。 无论我尝试过什么方法——我要么得到了包含所有 GUI 的图像,要么得到了剪切图。 知道如何才能获得 2 张好图像吗?
最佳答案
您可以循环遍历所有轴并调用 getframe
得到那个轴。然后,您可以使用 imwrite
保存 cdata
。
% Get a list of all axes in the figure
allax = findall(gcf, 'type', 'axes');
for k = 1:numel(allax)
% Get the axes as an image
fr = getframe(allax(k));
% Save the image
imwrite(fr.cdata, sprintf('%d.png'));
end
如果您已经有轴 handle ,则可以直接使用它们
fr = getframe(axes2);
imwrite(fr.cdata, 'axes2.png')
fr = getframe(axes1);
imwrite(fr.cdata, 'axes1.png')
如果你想包含 X 和 Y 轴标签,你可以这样做
function axes2image(ax, filename)
hfig = ancestor(ax, 'figure');
rect = hgconvertunits(hfig, get(ax, 'OuterPosition'), ...
get(ax, 'Units'), 'pixels', get(ax, 'Parent'));
fr = getframe(hfig, rect);
imwrite(fr.cdata, filename);
end
axes2image(axes2, 'axes2.png')
axes2image(axes1, 'axes1.png')
关于matlab - 将 GUI 中的轴保存为图像 MATLAB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38954777/