How to use savefig in app-designer?

Here is how I plot my map in app-designer:
load map_use; %M N
[m n] = size(M);
for i=1:200;
M2 = [];
for j=1:m;
if M(j,3)==i;
M2=[M2;M(j,1:2)];
end
end
patch(app.mapPlot, M2(:,1), M2(:,2), [0.62,0.83,0.50]);
end;
savefig(app.mapPlot, 'AA.fig'); %Line 43
but get this error:
Error using savefig (line 43)
H must be an array of handles to valid figures
So how do I properly use savefig and openfig within app-designer?
Many thanks!

 Risposta accettata

Cameron B
Cameron B il 9 Feb 2020
Modificato: Walter Roberson il 9 Feb 2020

1 voto

I’d use the function created by Adam Danz found on https://www.mathworks.com/matlabcentral/answers/281318-how-can-i-save-a-figure-within-app-designer It worked well for me. You can use this and then use the saveas function to save your new figure. I suppose you can set the visibility of your new figure to ‘off’ if need be, but you’ll probably have to do that yourself.

9 Commenti

Many thanks!
I'm reading the documentation of CopyUIAxes, but still can't quite figure it out. So, 'h = copyUIAxes(uiax)' stores the current figure to h or it actually is the command to copy the figure to another axes? I need a way to save the the entire figure info into a file so that I can load it back and redraw the figure quickly again. The new figure should allow me to reset things like the xlim, ylim, and draw more points to it, etc.
fh = uifigure();
% uiax = uiaxes(fh);
% hold(uiax,'on')
% grid(uiax,'on')
% x = linspace(0,3*pi,200);
% y = cos(x) + rand(1,200);
% ph = scatter(uiax,x,y,25,linspace(1,10,200),'filled');
% lh = plot(uiax, x, smooth(x,y), 'k-');
% title(uiax,'copyUIaxes Demo','FontSize',18)
% xlabel(uiax,'x axis')
% ylabel(uiax,'y axis')
% zlabel(uiax,'z axis')
% cb = colorbar(uiax);
% cb.Ticks = -2:2:12;
% caxis(uiax,[-2,12])
% ylabel(cb,'cb ylabel')
% lh = legend(uiax,[ph,lh],{'Raw','lowess'},'Location','NorthEast');
% drawnow()% all graphics to catch up before copying
%
% % Ex 1: Copy axis to another figure
% h = copyUIAxes(uiax);
%
% % Ex 2: specify ax axis as destination
% fNew = figure();
% axh = subplot(2,2,1,'Parent',fNew);
% h = copyUIAxes(uiax,axh);
Adam Danz
Adam Danz il 9 Feb 2020
Modificato: Adam Danz il 9 Feb 2020
You can export the UIAxes from your app into an independent figure which can be saved.
If your axes are named app.UIAxes, you can run
newFig = figure();
figHandle = copyUIAxes(app.UIAxes, newFig)
Then you can run savefig(newFig, 'figureName.fig').
Leon
Leon il 9 Feb 2020
Modificato: Leon il 9 Feb 2020
Many thanks, Adam!
So I save my figure like this:
figHandle = copyUIAxes(app.mapPlot2);
savefig(figHandle, 'AA.fig');
For the savefig line, I got the error. Do I need to initialize a figHandle like app.Plot within app-designer first? How do I create a hidden one like that?
Error using savefig (line 43)
H must be an array of handles to valid figures.
Then when I want to load it later on, I do this?
openfig('AA.fig');
copyUIAxes(app.mapPlot2); % load the figure back to app.mapPlot2
What really confuses me is the use of copyUIAxes. It seems to be designed to copy a figure information among several parallel figures, instead of saving the info into a file for future use on the same figure. Right now, in order to do the latter, it requires the creation of an independent figure, which may not be convenient, especially within app-designer.
Would you please consider to create a pair of them, i.e., SaveUIAxes (into a fig file, or an app.xxx property), and OpenUIAxes (paste the saved fig into a new handle).
Adam Danz
Adam Danz il 9 Feb 2020
Modificato: Adam Danz il 9 Feb 2020
Sorry, I updated my comment with correct info.
The purpose of copyUIAxes is to copy a UIAxes (from app designer, for example) into a new figure. That way you can save that figure.
To load the data from an independent figure back into the app uiaxes,
figHandle = openfig('AA.fig');
axHandle = gca(figHandle);
copyobj(axHandle.Children, app.UIAxes)
Although it would be better to save the data to a mat file that you can load and then send back into the plotting function within the app.
Thank you! Sorry for the many questions!
I'm able to save the figure now:
newFig = figure();
figHandle = copyUIAxes(app.mapPlot2, newFig)
savefig(newFig, 'Map.fig');
but when I try to open the file and copy the figure back to my app.mapPlot2 handle again, there is an error:
h = openfig('Map.fig');
copyUIAxes(h, app.mapPlot2)
Error:
Error using copyUIAxes
The value of 'uiax' is invalid. It must satisfy the function:
@(h)isa(h,'matlab.ui.control.UIAxes').
Error in copyUIAxes (line 112)
parse(p,varargin{:})
Error in QC_profile_working_latest/StartupFcn (line 882)
copyUIAxes(h, app.mapPlot2)
Error using matlab.apps.internal.GraphicsCallbackProxy/runCallbackFunction (line 23)
Error while evaluating GraphicsCallbackProxy CallbackFcn.
Adam Danz
Adam Danz il 9 Feb 2020
Modificato: Adam Danz il 9 Feb 2020
No problem with the questions ;)
copyUIAxes only copies from UIAxes. The axes in the figure you opened are regular axes.
I updated my previous comment showing how to copy from the figure back into your app using copyobj()
Leon
Leon il 9 Feb 2020
Hooray...... Working now!
Thank you so much for your time. Enjoy the beautiful Sunday!
Thanks! Feel free to leave a rating and comment on the file exchange page if you'd like.
Leon
Leon il 9 Feb 2020
Just did, Adam! Thanks again for your help.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Printing and Saving in Centro assistenza e File Exchange

Prodotti

Release

R2019b

Richiesto:

il 8 Feb 2020

Commentato:

il 9 Feb 2020

Community Treasure Hunt

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

Start Hunting!

Translated by