How to create a push button to stop run a function in MATLAB GUI?
8 次查看(过去 30 天)
显示 更早的评论
Hi,
I made a GUI in MATLAB. It has a start button to run a ".m" file (a function in MATLAB), and shows tow images (as outputs). The start button works, and the GUI can show the outputs. I need to create another push button in GUI in order to stop run the function (every time I push it, it stops the program). I searched on the Internet, and I just found solutions for interrupting a for or while loop not a function. How can I create the stop bush button?
I appreciate any help in advance.
Thank you,
Abbas
0 个评论
回答(1 个)
Walter Roberson
2017-7-13
Okay, what you have to do is this:
Have your first GUI start a second copy of MATLAB to run the function that has to be interrupted. Set up anything necessary for the original MATLAB and the second MATLAB to communicate back and forth. Have the stop button be present in the original MATLAB, not the one running the function. When the stop button is detected in the first MATLAB, have the first MATLAB session kill the process that the function is running in. On MS Window, this would involve using system() to invoke "taskkill"; on OS-X or Linux, this would involve using system() to invoke "kill".
If you do not wish to create a second MATLAB session and kill the process like this, then there is no way to do what you want. MATLAB does not permit functions to be interrupted against their will. The methods for interrupting for loops and while loops are with the cooperation of the loop, with the loop checking from time to time whether it is being asked to be interrupted.
Ah, I just thought of another possibility. If you have the Parallel Computing Toolbox, then instead of starting a new MATLAB session, you can use parfeval() to run the function that needs to be interruptible. Then, when the button press is detected, you can issue a cancel() against the "future" that was returned by parfeval().
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!