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

1 view (last 30 days)
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.

Answers (1)

Hari
Hari on 3 Sep 2024
Edited: Hari on 3 Sep 2024
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!

Tags

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!