File Exchange Pick of the Week

Our best user submissions

A new way to build GUIs

MATLAB 7 introduced a couple of new function types to MATLAB – nested functions and anonymous functions. As their name (hopefully) implies, a nested function is a function that is literally nested inside of another function. i.e.,

function foo
   function my_nested_function
   ...
   end % my_nested_function
end % foo

One of the cool things about nested functions is that the inside functions can access all variables from the workspace of the outer function. This can greatly reduce the need to pass variables between functions.

This can be useful when building GUIs. If you’ve used GUIDE, you are surely familiar with the handles structure that is used to allow each subfunction to access all GUI components. Trying to extend your GUI to use multiple windows can get pretty tricky pretty fast. If the windows share a lot of data, you can end up with multiple copies of large data sets. With nested functions, each callback can access all of the other handles in your GUI, without having to pass around the handle structure. This greatly simplifies building multiple-window UIs, and eliminates the memory overhead of passing data between windows.

Trust me on this last point – I went head-to-head with a lead developer of nested functions (Loren Shure). She built a “simple” 2 window UI to manipulate large images using nested functions, and challenged me to do the same in GUIDE. Her code was much more compact, memory efficient, and she wrote it more quickly.

Anyay, on to this week’s pick – Steve Lord was kind enough to submit a couple of examples showing how to use nested functions to build GUIs. Check it out – maybe this will give you some interesting idea of your own on how to make use of nested functions.

|
  • print
  • send email

コメント

コメントを残すには、ここ をクリックして MathWorks アカウントにサインインするか新しい MathWorks アカウントを作成します。