Matlab GUI应用软件工程与模式? [英] Software engineering and patterns in Matlab GUI application?

查看:61
本文介绍了Matlab GUI应用软件工程与模式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我被要求使用Matlab和GUIDE开发一个简单的GUI应用程序,它将与单独的后端引擎(也是Matlab)进行交互。我来自Java和.Net背景。



Matlab文档对如何在技术上做到这一点很强大,但对工程过程说的很少,特别:




  • 如何打包/命名空间功能或创建库?



  • 如何实现典型的GUI模式,例如:MVC,观察者?


  • 这些问题在Matlab开发中是否有意义?


  • 任何其他常见的成语/陷阱? / p>




直接建议或指向良好参考资料的指针将非常有帮助。谢谢。

解决方案

这是一个完全合理的问题,有点谨慎,可以编写简单的GUI代码维持。一些建议:




  • 将GUI布局代码和业务逻辑放在不同的m文件中。 (有关示例代码,请参见此FileExchange提交)。相关地,使用嵌套函数来维护GUI状态,而不是传递使用setappdata和getappdata或传递结构。


  • 一般来说,使用嵌套匿名函数用于回调。嵌套函数类似于Java中的内部类,匿名函数实现词法闭包(如Ruby中的块)。


  • 使用例外正常处理错误。


  • 给每个GUI对象(例如滑块,轴)赋予一个独特而有意义的标签。例如,frequencySlider或dataAxes。这有助于加强GUI布局和逻辑之间的分离。


  • GUIDE提供的gui控件是相当低级别的,但您可以实现可重用的更高级别通过为各种控件选择系统标签名称(例如,滑块的频率滑块和相关文本标签的频率标签)组件。组件初始化程序可以使用findobj查找组件的各个部分并对其进行初始化。例如




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

%回调的嵌套函数;注意使用sliderHandle
函数sliderCallback(h,e)
fprintf('current value is%g\\\
,get(sliderHandle,'Value'));
end

end




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.

解决方案

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:

这篇关于Matlab GUI应用软件工程与模式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆