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

1 回表示 (過去 30 日間)
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

採用された回答

Kojiro Saito
Kojiro Saito 2019 年 5 月 22 日
編集済み: Kojiro Saito 2019 年 5 月 23 日
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 件のコメント
Kojiro Saito
Kojiro Saito 2019 年 5 月 23 日
I've just added another way in my previous answer which does not save any file.
goatface killa
goatface killa 2019 年 5 月 24 日
Thanks again,
It's good to know that this sort of workaround exists.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by