how to close a guide figure but close it without deleting the output struct

6 views (last 30 days)
i have two files one being a fig set up with the guide and one that is suposed to take the fig file and use the user inputed numbers to calculate stuff.
the fig file creates a struct of said numbers and i can get the fig to run in the other script file but when i try and call on one of those numbers entered it says its trying to call on a deleted figure
function fpok_Callback(hObject, eventdata, handles)
%guidata(hObject,handles);
clc
% fpmcstr=string(get(handles.chkfpmc,'string'))
% fpcoystr=string(get(handles.chkfpcoy,'string'))
% fprgstr=string(get(handles.chkfprg,'string'))
% fpblkstr=string(get(handles.chkfpblk,'string'))
% fpmcbstr=string(get(handles.chkfpmcb,'string'))
% fparidstr=string(get(handles.chkfparid,'string'))
%
fpres=struct('fpmcqt',0,'fpcoyqt',0,'fprgqt',0,'fpblkqt',0,'fpmcbqt',0,'fparidqt',0);
if get(handles.chkfpmc,'Value')
fpmcqt1=handles.fpmcqt.String;
fpres.fpmcqt=str2double(fpmcqt1)
end
if get(handles.chkfpcoy,'Value')
fpcoyqt1=handles.fpcoyqt.String;
fpres.fpcoyqt=str2double(fpcoyqt1)
end
if get(handles.chkfprg,'Value')
fprgqt1=handles.fprgqt.String;
fpres.fprgqt=str2double(fprgqt1)
end
if get(handles.chkfpblk,'Value')
fpblkqt1=handles.fpblkqt.String;
fpres.fpblkqt=str2double(fpblkqt1)
end
if get(handles.chkfpmcb,'Value')
fpmcbqt1=handles.fpmcbqt.String;
fpres.fpmcbqt=str2double(fpmcbqt1)
end
if get(handles.chkfparid,'Value')
fparidqt1=handles.fparidqt.String;
fpres.fparidqt=str2double(fparidqt1)
end
handles.output=fpres;
%guidata(hObject,handles);
uiresume(ancestor(hObject, 'figure'))
close()
%save(Flatpack, 'fpres.FlatpackMC')
when the ok button is puched this runs, and
fp=questdlg('Flatpacks','Select','Yes','No','c');
switch fp
case 'Yes'
fpres=Flatpack;
otherwise
fpres=struct('fpmcqt',0,'fpcoyqt',0,'fprgqt',0,'fpblkqt',0,'fpmcbqt',0,'fparidqt',0);
end
if ~ishandle(Flatpack)
fpmcqt=fpres.fpmcqt;
end
this is what i have in the other script to call the fig and the numbers entered in it. ther is only fpmcqt because i have been just testing the one to see if it can work.

Answers (2)

Sameer
Sameer on 22 May 2024
Hi Joseph
Closing a figure in MATLAB results in the deletion of its associated data from memory, including the contents of the handles structure. Consequently, attempting to access properties such as “handles.fpmcqt.String” after the figure's closure will lead to errors, as MATLAB is unable to locate the requested information.
A viable solution to this issue involves the utilization of the “uiwait” and “uiresume” functions. These functions can effectively pause the execution of code until the user has completed their data input within the figure. Here's an implementation strategy:
Prior to invoking the figure within the main script, incorporate uiwait to pause execution:
fp = questdlg('Flatpacks', 'Select', 'Yes', 'No', 'c');
switch fp
case 'Yes'
uiwait(Flatpack); % Execution pauses here until uiresume is called
fpres = guidata(Flatpack); % Assuming Flatpack is the figure handle and data is retrieved after resuming
otherwise
fpres = struct('fpmcqt', 0, 'fpcoyqt', 0, 'fprgqt', 0, 'fpblkqt', 0, 'fpmcbqt', 0, 'fparidqt', 0);
end
In the fpok_Callback function, invoke uiresume prior to closing the figure:
handles.output = fpres;
guidata(hObject, handles); % Ensure data is saved before resuming
uiresume(ancestor(hObject, 'figure')); % Resumes execution of the main script
close(); % Closes the figure
By pressing the OK button, “uiresume” is executed, thereby allowing the main script to proceed. Importantly, at this juncture, the figure remains open, ensuring that its data is accessible. The figure is subsequently closed only after the necessary data has been accessed.
Please refer to the below links for more information:
I hope this helps!
Sameer

Image Analyst
Image Analyst on 22 May 2024
I believe you need to uncomment the guidata() line. This will update the global handles structure. Otherwise you've added the fpres structure as a new field to the handles structure but it's only local to that function. You have a copy of handles that has things that can be seen only while that function is running. Once you leave that function, that local copy of handles vanishes along with any changes you made to it. To keep those changes once the function exits you must use guidata() to transfer the new local changes to the global master copy of handles.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!