Saving an axes as jpg file using saveas
44 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
nl2605
am 18 Jul. 2013
Beantwortet: Prajwol Tamrakar
am 6 Okt. 2023
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.
0 Kommentare
Akzeptierte Antwort
Dishant Arora
am 18 Jul. 2013
F = getframe(handles.axes1);
Image = frame2im(F);
imwrite(Image, 'Image.jpg')
5 Kommentare
Adryan Fauzi
am 5 Jul. 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
am 5 Jul. 2023
Bearbeitet: DGM
am 5 Jul. 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')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1427413/image.png)
% capture the figure
figscreenshot = frame2im(getframe(gcf));
imwrite(figscreenshot,'figscreenshot.png')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1427418/image.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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1427403/image.png)
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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1427408/image.png)
Weitere Antworten (1)
Prajwol Tamrakar
am 6 Okt. 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.
SOURCE and FULL Credit: https://www.mathworks.com/matlabcentral/answers/362358-how-do-i-take-a-screenshot-using-matlab
% 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')
0 Kommentare
Siehe auch
Kategorien
Mehr zu Display Image finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!