How to save GUI axes ?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File 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!