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.