I"m trying to use "questdlg" to shows question dialog with several buttons. My aim is that when I click "stop" button in the dialog generated by using "questdlg", no further execution of code."switch" is used to define each action of buttons right after "questdlg".

You are watching: How to stop a code in matlab

I"m using break and return and they"re all just to get out of the "switch". How can I abort all code running by using command?


return only returns from the current function. If you are servicing a callback for a pushbutton, you are just going to return from the callback, without having affected anything in the loop of code that is executing.
The context of the question is that the user has a calculation loop and wishes to be able to click a button and have the calculation loop interrupted. When graphics interruptions are serviced it is in a callback function. The question then becomes:
is there a way for the callback function to force the calculation function to abort?if not, is there a way for the callback function to cooperate with the calculation function to have the calculation function abort?
The answer for the first of those is "NO": the only two ways to force something to stop without its cooperation are to
quit or exit MATLAB; oruse java robot or similar to emit control-C into the command line to force the program to stop running
The answer to the second of those possibilities is "Yes, it can be done" -- and the methods for doing that are what I discussed in my Answer.

error() will return to the closest "try" upwards in the calling stream. If you are inside a callback, the calling stream probably is not very deep.
If you are in nested loops and want to exit out of several of them, then you should define a "quitthis" variable that is tested right after the end of each loop, issuing a "break" if detected, and in this way having the quitting cascade out of all of the loops.
If you have a loop that is executing and you want to be able to interrupt it with a GUI action, then you need to have the GUI set a variable in a location that the loop polls the value of periodically to determine whether it is being told to quit. The termination must be cooperative: there is no way for a GUI element to force a loop to quit. For example,
Notice I used a toggle, not a pushbutton. pushbuttons have their value reset to 0 right after their callback is taken.


As already mentioned by Walter Roberson there no know solution yet. In fact, I wrote a function "dbexit", which finally works with
I think wgc2010.org should provide this possibility to terminate a program cleanly or add an option in the error function, which prints only one line without the word "error" - and without the whole function hierarchy.

Community Treasure Hunt

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

Start Hunting!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.


Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

Select web site

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other wgc2010.org country sites are not optimized for visits from your location.

See more: Should Preschool Pencils ? The 10 Best Pencils For Kindergarteners


Accelerating the pace of engineering and science

wgc2010.org is the leading developer of mathematical computing software for engineers and scientists.