CloseRequestFcn of GUI not working with wrong current folder

6 ビュー (過去 30 日間)
Peter Seibold
Peter Seibold 2021 年 1 月 24 日
コメント済み: Walter Roberson 2021 年 1 月 26 日
I have in the GUI m-file a 'figure1_CloseRequestFcn'. This works fine as long the current Matlab folder is not changed.
When I run another file from another folder, I cannot quit Matlab unless I make the GUI-folder to the current folder.
With the standard CloseRequestFcn in the GUI property I can allways quit Matlab, even if the current folder is not the GUI folder.
Is there a way to insert into the GUI-figure-property something like:
'try;MyGUI(''figure1_CloseRequestFcn'',hObject,eventdata,guidata(hObject));catch;closereq;end;' ?
I do not want to change anything like the search path in Matlab.

採用された回答

Walter Roberson
Walter Roberson 2021 年 1 月 24 日
You have coded figure1_CloseRequestFcn in a way that calls upon a function that you expect to be in the MATLAB path, but turns out not to be because you relied upon it being in the current directory instead of creating a specific MATLAB path entry for the appropriate directory.
What you can do, is in the OpenFcn for the GUI, use mfilename() to detect where the .m file for the GUI itself is located. fileparts() to get the directory, and cd to that directory, recording the old directory. Now take the handle of the function that you need in the close request function, and store it in handles; then cd back to the old directory.
Then in the close request function, pull the handle of the function out of the handles structure, and use it to call the needed function, instead of counting on the function being in the current directory. When you construct a handle of a function using @ then MATLAB remembers the directory the code is located in and so knows where to find it.
  6 件のコメント
Peter Seibold
Peter Seibold 2021 年 1 月 25 日
Walter, thank you very much for your help! I understand now much more what is going on with '@'.
To make it clear for other users how I solved the problem, here my code:
function TestCloseReq_OpeningFcn(hObject, eventdata, handles, varargin)
set(handles.figure1,'CloseRequestFcn',{@CloseTestCloseReq,handles});
handles.output = hObject;
guidata(hObject, handles);
function CloseTestCloseReq(hObject, eventdata, handles)
disp('Closing')
delete(hObject);
Both functions are inside of the GUI m-file.
Now you can run the GUI, change the working directory and close the GUI figure.
Walter Roberson
Walter Roberson 2021 年 1 月 26 日
Yup, that should work fine, even if the GUI's .m was invoked whent the user is cd'd to another directory.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by