How can I save UIAxes as an image when button is pushed in app designer?

42 Ansichten (letzte 30 Tage)
How can I save UIAxes as an image when button is pushed in app designer?
newfigure = figure;
copyobj(app.UIAxes, newfigure);
hgsave(newfigure, 'testFIgure.fig');
I tried this but it gives me an error.
  3 Kommentare
Yaren Sever
Yaren Sever am 4 Jul. 2020
2020 release. I solved the problem but now when I push the button, XLabel, YLabel and Title of the UIAxes dissappears. Do you have any idea about this?
% Create new axis
newAxes = axes;
% Copy all objects from UIAxes to new axis
newAxes.Title= app.UIAxes.Title;
newAxes.XLabel = app.UIAxes.XLabel;
newAxes.YLabel = app.UIAxes.YLabel;
copyobj(app.UIAxes.Children, newAxes)
fig2Save = ancestor(newAxes, 'figure');
fig2Save.Visible='off';
%dialog box,save as
filter = {'*.jpg';'*.png'}
[filename, pathname] = uiputfile(filter);
newfilename = fullfile(pathname, filename);
saveas(fig2Save, newfilename);
Yaren Sever
Yaren Sever am 4 Jul. 2020
I solved whole problem:
figure;
% Create new axis
newAxes = axes;
% Copy all objects from UIAxes to new axis
newAxes.Title= app.UIAxes.Title;
newAxes.XLabel = app.UIAxes.XLabel;
newAxes.YLabel = app.UIAxes.YLabel;
copyobj(app.UIAxes.Children, newAxes)
%in order to keep titles and labels on uiaxes
xlabel(app.UIAxes, newAxes.XLabel.String);
ylabel(app.UIAxes, newAxes.YLabel.String);
title(app.UIAxes, newAxes.Title.String);
fig2Save = ancestor(newAxes, 'figure');
fig2Save.Visible='off';
%saveas(fig2Save,'myfigg.png')
%close(fig2Save);
%dialog box,save as
filter = {'*.jpg';'*.png'}
[filename, pathname] = uiputfile(filter);
newfilename = fullfile(pathname, filename);
saveas(fig2Save, newfilename);

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Image Analyst
Image Analyst am 3 Jul. 2020
Try exportgraphics().
  3 Kommentare
Yaren Sever
Yaren Sever am 4 Jul. 2020
2020 release. I solved the problem but now when I push the button, XLabel, YLabel and Title of the UIAxes dissappears. Do you have any idea about this?
% Create new axis
newAxes = axes;
% Copy all objects from UIAxes to new axis
newAxes.Title= app.UIAxes.Title;
newAxes.XLabel = app.UIAxes.XLabel;
newAxes.YLabel = app.UIAxes.YLabel;
copyobj(app.UIAxes.Children, newAxes)
fig2Save = ancestor(newAxes, 'figure');
fig2Save.Visible='off';
%dialog box,save as
filter = {'*.jpg';'*.png'}
[filename, pathname] = uiputfile(filter);
newfilename = fullfile(pathname, filename);
saveas(fig2Save, newfilename);

Melden Sie sich an, um zu kommentieren.


Adam Danz
Adam Danz am 24 Sep. 2020
The code you shared shows that you're exporting the children of a UIAxes onto a regular axis.
That can be done with copyUIAxes() from the file exchange, even on Matlab releases prior to 2020. That function avoids the problem of disappearing axis labels.
  2 Kommentare
zsh fardi
zsh fardi am 5 Jan. 2022
Bearbeitet: Image Analyst am 5 Jan. 2022
Hi. Why is copyUIAxes() undefined in Matlab R2021a?
Image Analyst
Image Analyst am 5 Jan. 2022
Bearbeitet: Image Analyst am 5 Jan. 2022
@zsh fardi Because it's a third party function written by Adam. It's not part of base MATLAB. You have to download it, and if it's not in your current folder, use the "Set Path" button on the Home tab of the tool ribbon to add its folder to the path.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Printing and Saving finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by