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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive 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