MATLAB Spoken Here

Using UIWAIT/UIRESUME to control program flow 2

Posted by Michael Katz,

When you run a MATLAB callback from a GUI that callback will normally run to completion. There are times when you may want to temporarily suspend execution until the user does something: such as input data, upload a file, connect or update connected hardware, acknowledge a message, etc. For those situations, you can use the uiwait and uiresume pair of functions.

UIWAIT is like pause, but instead of suspending a program until the user presses “Enter”, it will suspend the running MATLAB program until another function calls UIRESUME. This is usually done by the callback of a dialog box or GUI. This gives you great flexibility in determining what conditions will cause the program to resume, and how you want to present that information to your end users. You can combine uiwait with modal dialogs in order to restrict the user’s interaction with any part of MATLAB until he activates the callback that calls uiresume. You can make any figure into a modal window by setting its WindowStyle property to 'modal'.

UIWAIT also accepts a timeout parameter. This way if the user does not or is unable to respond within a certain time, you can have the program either abort or continue execution in a meaningful way.

My one warning is not to overuse this function. If at all possible, you should design your GUIs so that user isn’t forced to deal with something right away if he doesn’t have to. Also, if you just want the user to enter a some data, select an item from a list, or press “OK” you can use one of the many synchronous dialogs I’ve already mentioned instead of creating a more complex UI.

2 CommentsOldest to Newest

I would like to point out the very similar built-in function duo uisuspend and uirestore, which are similar in functionality and name to the uiwait and uiresume functions presented above. It is important not to confuse between these two function sets, and most importantly, not to confuse between uiresume and uirestore after using their counterparts earlier in the code – odd things might happen if you do…

Also, note that in Matlab releases up to R2010b, uirestore has a minor bug in that it overwrites the figure’s WindowScrollWheelFcn callback with the WindowKeyPressFcn value ( http://www.mathworks.com/support/bugreports/646025 ). This bug was finally fixed in R2011a.

IMHO, it would be better to modify uisuspend and uirestore to simply call their documented counterparts uiwait and uiresume, because the current situation can easily cause confusion.

These postings are the author's and don't necessarily represent the opinions of MathWorks.