MATLAB Community

MATLAB, community & more

Using UIWAIT/UIRESUME to control program flow

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.

|
  • print

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.