How do I store a figure within App Designer such that it can be replotted at will by a button press?

1 visualizzazione (ultimi 30 giorni)
I have a simple GUI created with App Designer (version 2018a) which calls a custom function to spawn a figure showing some data. I'd like to be able to respawn this figure at will after I've closed it.
I assigned the figure a handle in the function and successfully passed it back into the app. I then assign it to a property variable within the app for later reference. Within the same callback that calls my function, the app is able to replot my figure using both the handle passed back as well as with the property variable. However, in the callback for my "Replot" button, the property value has reverted to a blank state. Is there a (succinct) way to preserve figure objects through multiple callbacks in App Designer?
sample code:
start 1st button push callback
[orig_fig1]=my_func(data); %Creates and displays a figure
app.figurestorage=orig_fig1; %Storing the figure as a private property in the app
figure(app.figurestorage) %This successfully recreates the same figure as generated within the "my_func" function
end 1st button push callback
start 2nd button push callback
fig1=app.figurestorage;
figure(fig1)
end 2nd button push callback
I get the following error from the 2nd button push:
fig1 =
handle to deleted Figure

Risposta accettata

Kojiro Saito
Kojiro Saito il 22 Mag 2019
Modificato: Kojiro Saito il 23 Mag 2019
You cannot access to a deleted figure, so how about saving it as a fig file first?
start 1st button push callback
[orig_fig1]=my_func(data); %Creates and displays a figure
app.figurestorage=orig_fig1; %Storing the figure as a private property in the app
figure(app.figurestorage) %This successfully recreates the same figure as generated within the "my_func" function
savefig('myFig.fig')
end 1st button push callback
start 2nd button push callback
openfig('myFig.fig')
end 2nd button push callback
UPDATED
Here is a way without creating any file.
start 1st button push callback
[orig_fig1]=my_func(data); %Creates and displays a figure
app.figurestorage=orig_fig1; %Storing the figure as a private property in the app
figure(app.figurestorage) %This successfully recreates the same figure as generated within the "my_func" function
% Change close behavior from deleting a figure to makiig it invisible
set(app.figurestorage, 'CloseRequestFcn', {@myCloseFcn, app});
% Local function definition
function myCloseFcn(src, eve, app)
app.figurestorage.Visible = 'off';
end
end 1st button push callback
start 2nd button push callback
figure(app.figurestorage)
end 2nd button push callback
  3 Commenti

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Prodotti


Release

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by