Saving an axes as jpg file using saveas

41 visualizzazioni (ultimi 30 giorni)
Hallo All
I have been trying to save an axes object on an gui as jpg file but it takes up weird pictures, like sometime the photo has only the centre part of the axes and sometimes it doesn't save the picture of the axes at all instead it takes picture of the pushbuttons besides the axes (in the GUI). Have been trying to set this error for a long time.
Thanks a lot.

Risposta accettata

Dishant Arora
Dishant Arora il 18 Lug 2013
F = getframe(handles.axes1);
Image = frame2im(F);
imwrite(Image, 'Image.jpg')
  5 Commenti
Adryan Fauzi
Adryan Fauzi il 5 Lug 2023
hi Dishant Arora, i have tried this code and successfully export my axes to image, but my image doesn't include axes title and axis description. how do I get the exported image to include both of those things?
DGM
DGM il 5 Lug 2023
Modificato: DGM il 5 Lug 2023
If you want to capture the axes, capture the axes. If you want to capture everything in the figure, capture the figure.
% some plot in an axes in a figure
x = 1:10;
plot(x)
title('blah')
xlabel('blah')
ylabel('blah')
% capture just the axes
axscreenshot = frame2im(getframe(gca));
imwrite(axscreenshot,'axscreenshot.png')
% capture the figure
figscreenshot = frame2im(getframe(gcf));
imwrite(figscreenshot,'figscreenshot.png')
If you have multiple axes or figures, or if you're building a GUI, use the appropriate handles instead of gca/gcf. If trying to do this inside a GUI, there may be other complications.
Also, unless you have some extraordinary needs, don't use JPG. For images like this, you'll get garbage results, and the files will typically be larger than a lossless PNG.
This is a 3x enlarged screenshot saved as PNG. The lines are crisp and colorful. The text is clear.
This is the same image saved as JPG and enlarged. The lines and text are blurry and surrounded by artifacts. The line colors are all muddy. The file size is tripled. Why would you intentionally do this?

Accedi per commentare.

Più risposte (1)

Prajwol Tamrakar
Prajwol Tamrakar il 6 Ott 2023
When using App Designer and you do not have the latest version 2020 or later, it is literally IMPOSSIBLE to save an image from the figure (app.UIAxes). It seems easy previously (of course easy when using GUIDE or just simple code, but Not for App Designer 2019a version). I wasted lots of hours trying to find a solution for this. The best solution ever found was - just get the screen short using the following code.
% Take screen capture
robot = java.awt.Robot();
pos = [0 0 400 400]; % [left top width height]
rect = java.awt.Rectangle(pos(1),pos(2),pos(3),pos(4));
cap = robot.createScreenCapture(rect);
% Convert to an RGB image
rgb = typecast(cap.getRGB(0,0,cap.getWidth,cap.getHeight,[],0,cap.getWidth),'uint8');
imgData = zeros(cap.getHeight,cap.getWidth,3,'uint8');
imgData(:,:,1) = reshape(rgb(3:4:end),cap.getWidth,[])';
imgData(:,:,2) = reshape(rgb(2:4:end),cap.getWidth,[])';
imgData(:,:,3) = reshape(rgb(1:4:end),cap.getWidth,[])';
% Show or save to file
imshow(imgData)
imwrite(imgData,'out.png')

Community Treasure Hunt

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

Start Hunting!

Translated by