MATLAB CTL-C GUI Cleanup

Hello, I have created a GUI to collect some data. This GUI uses some equipment that has to be connected/disconnected to MATLAB with commands. I want to make it so that when you push CTL-C to stop running something any of the functions, the equipment is disconnected properly. Is that possible without using "oncleanup" with putting it in every function used in the GUI?

回答 (1 件)

Walter Roberson
Walter Roberson 2018 年 6 月 25 日

0 投票

When you launch the GUI, have a function create an oncleanup, and then have the function uiwait() or waitfor() the gui figure. The control-C will trigger exiting the uiwait() or waitfor(), causing the function to exit, triggering the oncleanup .
... at least in theory.

5 件のコメント

Sarah Crimi
Sarah Crimi 2018 年 6 月 28 日
Honestly I still don't understand this answer. I made a function to check to see if the equipment is turned off that turns off the equipment if necessary. However, I have no idea where to call that function. I can't find a sort of function matlab created to close the GUI. The only thing that I can think is to create a button that does this. But this doesn't account for 'CTL-C' cases or cases where GUI is closed with red x button that is already on GUI.
Walter Roberson
Walter Roberson 2018 年 6 月 28 日
See https://www.mathworks.com/help/matlab/ref/matlab.ui.figure-properties.html#buiwuyk-1-CloseRequestFcn for controlling what happens when the figure closes including by clicking on the "X" window decoration.
If you are using GUIDE then look in the *OpeningFcn callback that gets created for your GUI. You will see a pair of lines like
% UIWAIT makes MyGUI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
Before those lines, put in a onCleanup call. Then, remove the comment marker from the second line, so it would look like
% UIWAIT makes MyGUI wait for user response (see UIRESUME)
uiwait(handles.figure1);
That will cause MATLAB to not return to the command prompt: instead it will wait for the figure to be closed or for control-C . When either of those happens then the *OpeningFcn would exit, triggering the onCleanup action.
Sarah Crimi
Sarah Crimi 2018 年 6 月 28 日
Ok, I tried this. It gives me an error though when I press 'CTL-C'
Warning: The following error was caught while executing 'onCleanup' class destructor:
Not enough input arguments.
> In MyGUI>MyGUI_OpeningFcn (line 60)
In gui_mainfcn (line 220)
In MyGUI (line 43)
Operation terminated by user during uiwait (line 81)
In MyGUI>MyGUI_OpeningFcn (line 82)
uiwait(handles.figure1);
Walter Roberson
Walter Roberson 2018 年 6 月 28 日
Hmmm, https://www.mathworks.com/matlabcentral/answers/156803-detection-of-ctrl-c-in-m-file suggests using onCleanup, which agrees with my memory, and when I test the code there it works. But I agree that the onCleanup does not seem to get invoked for ^C for waitfor() or uiwait() so I do not know what the solution is.
Sarah Crimi
Sarah Crimi 2018 年 7 月 3 日
thanks for your help :)

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

カテゴリ

ヘルプ センター および File ExchangeSimultaneous and Synchronized Operations についてさらに検索

タグ

質問済み:

2018 年 6 月 25 日

コメント済み:

2018 年 7 月 3 日

Community Treasure Hunt

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

Start Hunting!

Translated by