How to save GUI axes ?

2 次查看(过去 30 天)
han han
han han 2020-8-8
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.

回答(1 个)

Prathamesh
Prathamesh 2025-4-7
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.

类别

Help CenterFile Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by