How to save GUI axes ?
22 views (last 30 days)
Show older comments
I want to save the axes on the GUI, but he will save the entire window.
I want the axes part, what should I do?
code
function pushbutton1_Callback(hObject, eventdata, handles)
[FileName, PathName, ~] = uiputfile( ...
{'*.png'},...
'Save as');
ax1 = handles.axes1;
x = rand(10,1);
y = rand(10,1);
scatter(x,y,'^')
new=FileName(1:end-4);
saveas(ax1, new,'png')
The saved .png like the following, but I want the axes part, and I don’t want the pushbutton, panel, table... etc. on the GUI.

0 Comments
Answers (1)
Prathamesh
on 7 Apr 2025
I understand that you want to save just the axes of your plot as a PNG file, excluding any other elements like pushbuttons, panels, or tables. To achieve this, you can use MATLAB's ‘exportgraphics’ function.
Below is an example code demonstrating how to use ‘exportgraphics’:
function exportGraphicsExample
hFig = figure('Position', [100, 100, 400, 300], 'Name', 'Export Graphics Example', 'NumberTitle', 'off');
ax1 = axes('Parent', hFig, 'Position', [0.1, 0.3, 0.8, 0.6]);
x = rand(10, 1);
y = rand(10, 1);
scatter(ax1, x, y, '^');
title(ax1, 'Random Scatter Plot');
uicontrol('Style', 'pushbutton', 'String', 'Save Plot', ...
'Position', [150, 20, 100, 40], ...
'Callback', @(src, event) savePlot(ax1));
% Function to save the plot
function savePlot(ax)
[FileName, PathName] = uiputfile({'*.png'}, 'Save as');
% Check if the user selected a file
if isequal(FileName, 0) || isequal(PathName, 0)
disp('User canceled the file selection.');
return;
end
% Construct the full file name with path
fullFileName = fullfile(PathName, FileName);
% Use exportgraphics to save only the axes content
exportgraphics(ax, fullFileName, 'Resolution', 300);
disp(['Plot saved to ', fullFileName]);
end
end
for more details, please refer to MathWorks documentation
hope this helps.
0 Comments
See Also
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!