How to save a custom-tailored figure (image) into a jpeg (or other formats) files

조회 수: 1 (최근 30일)
Ali Minachi
Ali Minachi 2021년 7월 28일
편집: Hari 2024년 9월 3일
I generate a 2D array (double) in a gui program and I use imagesc to make a figure. Then the user has options to change and custom-make the figure (axis equal, tight, zoom in, colormap, change CLim, etc.). I would like to allow the user to save this custom-made figure as a jpg or other formats without the boarders and axes. I used imwrite, but I don't know how to apply the custom-made options. I would be grateful if anyone can provide me an answer. Thanks in advance.

답변 (1개)

Hari
Hari 2024년 9월 3일
편집: Hari 2024년 9월 3일
Hi Ali,
I understand that you have a GUI program that generates a 2D array and displays it using imagesc. Users can customize the figure's appearance, and you want to save this customized figure as an image file without borders and axes.
I assume you want to preserve the customizations (like zoom, colormap, axis adjustments) made by the user before saving the figure.
  • Capture the Figure: Use "getframe" to capture the currently displayed figure, including all user-made customizations.
% Assume 'hFig' is the handle to your figure
frame = getframe(hFig);
img = frame.cdata;
  • Remove Borders and Axes: If you want to remove axes and borders, ensure they are turned off before capturing.
% Turn off axes and borders
set(gca, 'Visible', 'off');
  • Save the Image: Use "imwrite" to save the captured image data to a file in the desired format (e.g., JPEG).
% Save the image as a JPEG file
imwrite(img, 'custom_figure.jpg');
  • Restore Axes and Borders: If needed, restore the axes and borders after saving.
% Restore axes visibility
set(gca, 'Visible', 'on');
  • Provide User Options: Allow the user to choose the file format and location using "uiputfile".
% Let the user choose file format and location
[file, path] = uiputfile({'*.jpg';'*.png';'*.tiff'}, 'Save Image As');
if ischar(file)
imwrite(img, fullfile(path, file));
end
References:
Hope this helps!

카테고리

Help CenterFile Exchange에서 Lighting, Transparency, and Shading에 대해 자세히 알아보기

태그

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by