Return data from GUIDE on figure close

44 次查看(过去 30 天)
I have a GUIDE generated UI in which I have wired up one of computed values, to handles and return it through automatically generated callback OutputFcn. When I run my UI, it computes the value and returns it to the command window immediately. The UI has interactive controls which will modify the output value, but it is not returned after the interactive controls are manipulated.
When I add uiwait(handles.figure1), then OutputFcn is not called until close, which is the desired behavior. Unfortunately when I do this the handles passed into OutputFcn is empty. When I omit the uiwait command from OpeningFcn, then the handles object passed to OutputFcn is populated as expected.
It seems that if OutputFcn is called while the figure is still open, then the handles object exists and can be referenced, however, if OutputFcn is not called until I close the figure, then the handles object points to a closed figure and I can no longer reference it.
Any advice to resolve this? Thanks!

采纳的回答

Adam Danz
Adam Danz 2019-9-13
编辑:Adam Danz 2019-9-14
To delay a GUIDE-GUI output until the GUI is closed, follow these steps. A functional demo is attached that produces an empty GUI and returns a hard-coded output when the GUI is closed. See comments in the m-file for help.
1) If your GUI does not already have a close request function, add one from within GUIDE by opening your GUI in GUIDE, right click the figure background, select View Callbacks, and the select CloseRequestFcn.
2) In the ..._OpeningFcn
function demoGui_OpeningFcn(hObject, eventdata, handles, varargin)
. . .
uiwait(); % add this to the end
3) In the ..._outputFcn
function varargout = demoGui_OutputFcn(hObject, eventdata, handles)
varargout{1} = handles.output;
delete(hObject); % add this
4) In the ..._CloseRequestFcn you created from GUIDE,
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% delete(hObject); % Remove this
handles.output = 3.1415926; % Produce or grab your outputs
guidata(hObject, handles); % Store the outputs in the GUI
uiresume() % resume UI which will trigger the OutputFcn
[Original answer has been edited]
  14 个评论
TallBrian
TallBrian 2019-9-13
Adam,
I missed that you moved the delete from the CloseRequest to the Output. This now works as I would like. Thank you very much, you are a wizard!
Adam Danz
Adam Danz 2019-9-13
If I were a wizard I would have understood your question from the getgo!
Glad I could help.
I'll update my answer so it's more useful.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2019-9-13
Below is a close request function I use in one project. Adapt as needed:
% --- Executes when user attempts to close figMainWindow.
function figMainWindow_CloseRequestFcn(hObject, eventdata, handles)
% hObject handle to figMainWindow (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: delete(hObject) closes the figure
global vidobj; % Video camera object.
try
% Save the current settings to our disk file.
SaveUserSettings(handles, true);
% Shut down the camera
try
delete(vidobj);
catch
end
% Get rid of variables from the global workspace so that they aren't
% hanging around the next time we try to run this m-file.
clear global;
delete(hObject);
catch ME
callStackString = GetCallStack(ME);
errorMessage = sprintf('Error in program %s.\nTraceback (most recent at top):\n%s\nError Message:\n%s', ...
mfilename, callStackString, ME.message);
set(handles.txtInfo, 'string', errorMessage);
WarnUser(errorMessage);
end
fprintf(1, '\n================= Exited figMainWindow_CloseRequestFcn. =================\n');
return; % from figMainWindow_CloseRequestFcn()
  3 个评论
Adam Danz
Adam Danz 2019-9-13
At the risk of sounding pedantic, the outputFcn returns the output to whatever workspace called the GUI. So if you call the GUI from the command window and included an output, the variable will be assigned to the base workspace. If the GUI is called from another function, the output is assigned to that function's workspace. Just FYI.
Image Analyst
Image Analyst 2019-9-14
I mainly use the close request function to capture situations where the user tries to shutdown the GUI via the x in the upper left of the title bar instead of from an "Exit" button I placed on the GUI. If the user does that (clicks on the X), and you don't have a close request function then the app will shutdown immediately, and if you had any shutdown or cleanup code in the exit button's callback function, then that code won't get implemented, for example shutting down camera(s) or other hardware, closing Excel workbooks, shutting down timers, or whatever you may want to do.
I just checked, and it seems like the OutputFcn() gets called immediately after the OpeningFcn() but does not get called during or after the CloseRequestFcn function, or if you delete the GUI's variable via a call to delete(). It's a good idea to put fprintf's in there so you know what functions get called when. For example:
% Print informational message so you can look in the command window and see the order of program flow.
fprintf('Just entered myApp_OutputFcn...\n');
try
% Get default command line output from handles structure
varargout{1} = handles.output;
% More code if you want....
% Maximize the window via undocumented Java call.
% Reference: http://undocumentedmatlab.com/blog/minimize-maximize-figure-window
MaximizeFigureWindow;
catch ME
% Some error happened if you get here.
callStackString = GetCallStack(ME);
errorMessage = sprintf('Error in program %s.\nTraceback (most recent at top):\n%s\nError Message:\n%s',...
mfilename, callStackString, ME.message);
WarnUser(errorMessage)
end
% Print informational message so you can look in the command window and see the order of program flow.
fprintf('Now leaving myApp_OutputFcn...\n');

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by