Swedish flagChinese (Simplified) flagEnglish flagGerman flagFrench flagSpanish flagHindi flag
June
28
2009

Matlab Gui with guihandles

David Gustafsson
david@techonomics.se


A small ad on techonomics.se

I have worked with a computer program in Matlab. The idea behind this is that it must be user friendly and efficient to do common calculations for some types of facilities. In this program I have used a lot of widgets that uipanel, uitree and uicontrol.

Many writers of tutorials for Matlab GUI recommend using Matlab's built-in function (or maybe I should call it a toolbox (?)) Called Guide. Type:

Guide >>

In the command window and it brings up a window where you can locate and configure the components of a figure. This has advantages for single-speed applications. However, the big problem if you want to write legitimate software. This is because it is difficult to keep track of your graphic objects. They are included in the exported figure ending in Figures

Place your components yourself

I have written my program without using the guide. It is easy to create and deploy components. As you can see below, requires a little command a lot of coding. Imagine how beefy codes for games like poker and other applications where there is a diversity of activities. Anyway, back to writing programs without using the guide. This is done by setting the window handles to be created in the figure and the position of the window. Of course, other parameters such as color or callback configured, just like inside the guide. I have written my programs like Matlab objects and used the strategy to outsource components of the constructor by calling a delivery function, tentatively named init gui. I save my object internally in the class in a structure that has hold of any levers to the components. This structure I initiate when I lay out the components.

  classdef aClassName <handle
    Properties
       handleToGui;
    end
    Methods
       function obj = aClassName (varargin)
          ...
          obj.initGui ();
          ...
       end
       ...
       Figure function = initGui (Obj)
          = Obj.handleToGui.fig figure (...);
          Obj.handleToGui.otherComponentName = ...
          ...
       end
    end
 % end classdef 

Matlab function guihandles

Matlab has a function called guihandles. It is fairly easy in many ways and I'll explain why. The reason is that you do not have to worry about adding your components directly into a structure when you put them out because guihandles (parent), provides a structure of handles to all child (children) who are in the figure. In calling for this, you can initiate handleToGui after all the components laid out. This then becomes a structure with field ames given the tag name (Tag) components have been given.

Problems with the Matlab function guihandles

However, there is a risk with this that I think Matlab has not thought of. Alternatively, it is so that I and others have used the all too carelessly. The fact is that the gui handles provide handles for all widgets in the figure. If you have different classes for different parts of your application, such as a separate class that handles a particular type of plottyta in a part of the program and another class that does a similar task in another place they can destroy each other if both are using guihandles. Guihandles in a subcomponent allows the handles to the main character (including other sub-components that are in it). If the second subcomponent has the same tag name of any component, they will be confused when the component invokes what it believes to be the component.

Do not use the Matlab function guihandles

Object-Oriented Programming to me is a programming method for creating good structure. This is by allowing each object (class) to fulfill his tasks internally and only communicate through specific interfaces with the environment. Global variables guihandles provides a potentially fatal risk. The concept of the world seeks in other words, for low coupling, high Cohesion and well-defined modules.

Authored by David Gustafsson, in: Programming | Tags: , , ,

No Comments »

RSS feed for comments on this post

Trackback URL

Leave a comment

The theme is modified from Aeros 2.0 - Blogglista.se - Translation is made ​​by N2H