views:

1662

answers:

3

I've been asked to develop a simple GUI application using Matlab and GUIDE, which will interact with a separate backend engine (also Matlab). I'm coming from a Java and .Net background.

The Matlab documentation is strong on how to technically do this, but says little about the "engineering" process, in particular:

  • How to package/namespace functions, or create libraries?

  • How best to lay files out on the filesystem?

  • How to implement typical GUI patterns eg: MVC, observer?

  • Whether these questions even make sense in Matlab development?

  • Any other common idioms/pitfalls?

Direct advice or a pointer to good reference material would be very helpful. Thanks.

A: 

I don't have experience with creating GUI's in matlab but if your company will spend mony on it matlab for .net may be a quicker option. It will allow you to write matlab code that will be rapped in a com component that can be called from .net which means you could write your GUI in any .net language you wanted. The link for it is below. http://www.mathworks.com/products/netbuilder/

Jared
Thanks for the suggestion, but the client is adamant that the UI is native Matlab code.
Dan Vinton
+2  A: 

I assume that you (Dan Vinton) have some knowledge in sw engineering and in design patterns an you are looking for the common practice in the MatLab world. For this purpose I would take a look at the GUI packages MatLab toolboxes such as optimization, curve fitting, etc. The GUI's of all these toolboxes come with their source code (they are regular MatLab scripts). You can also take a look at it or at the source code of several high-ranked GUI packages from MatLab Central. These will give you a good representation of common practice in MatLab.

bgbg
Your assumption is correct: I've happily been writing software for years conforming to certain practices that don't seem to port to a less OO platform like Matlab...
Dan Vinton
+5  A: 

This is a perfectly reasonable question, and with a bit of care it is possible to write GUI code that is easy to maintain. Some suggestions:

  • Put the GUI layout code and the "business logic" in different m-files. (see this FileExchange submission for example code). Relatedly, use nested functions to maintain GUI state instead of passing of using setappdata and getappdata or passing around a structure.

  • In general, use nested and anonymous functions for callbacks. Nested function are similar to inner classes in Java, and anonymous functions implement lexical closures (like blocks in Ruby).

  • Use exceptions to deal gracefully with errors.

  • Give each GUI object (e.g., slider, axes) a unique and meaningful tag. For example, "frequencySlider" or "dataAxes". This helps reinforce the separation between GUI layout and the logic.

  • The gui controls provided by GUIDE are fairly low-level, but you can implement reusable higher-level components by choosing systematic tag names for the various controls (e.g., 'frequencySlider' for a slider and the 'frequencyLabel' for the associated text label). The component initialization routine can use findobj to look up the various parts of the component and initialize them. E.g.

    function myComponent(fig, basename)
       sliderHandle = findobj(fig, 'tag', [basename 'Slider']);
       textHandle = findobj(fig, 'tag', [basename 'Label']);
       %  initialize ...
       set(sliderHandle, 'Callback', @sliderCallback);

       % nested function for callback; note use of sliderHandle
        function sliderCallback(h,e)
            fprintf('current value is %g\n, get(sliderHandle,'Value'));
        end

      end
  • If you are using R2008a or later, there is lot of additional support for OO development:

related questions