File Exchange Pick of the Week

HGSETGETPLUS – an extension to HGSETGET 2

Posted by Guest Picker,

Eric’s pick this week is “HandleGraphicsSetGet” by Andrew Newell.

Hello, my name is Eric, and I’ll be contributing to the Pick of the Week blog from time to time. This week is one such occasion! Like Jiro and Brett, I’m an Application Engineer here at MathWorks. I started using MATLAB in grad school for data filtering and statistics, and pretty soon I started looking for any reason I could to use it!

One of my favorite features in MATLAB is the object-oriented programming (OOP) system. It’s great for building complex GUIs, creating components to share with other people, and for managing large projects. A particularly useful feature of the OOP system in MATLAB is the ability to create classes that behave like Handle Graphics objects, with a SET/GET interface. To do this, you write a class that inherits from “hgsetget”, a built-in MATLAB class.

This week’s pick is a very useful extension to the hgsetget class that started out as a MATLAB Answers post by Andrew. Using inheritance, Andrew wanted to extend the hgsetget class to add an easy validation and enumeration interface for property values. Jiro shared some more of the background story:

“In case you were interested, Andrew wrote this entry in response to this MATLAB Answers question he posted a few weeks ago. When he mentioned he came up with a good solution, I encouraged him to post it on File Exchange. I’m glad he did, because it’s a great example of building on top of a base MATLAB class.”

After some dialog and feedback from Jiro, the end result was a new MATLAB class called “hgsetgetplus”. It allows you to easily constrain the set of values that properties in your class can assume, and to notify the user what those values are. To see how this can be useful, consider the behavior of built-in Handle Graphics objects in MATLAB:

>> h = surf(peaks); >> set(h, 'LineWidth', -5);
??? Error using ==> set Value must be finite and greater than zero

You can certainly implement behavior like this in your own classes. However, you must write a “set” method for each property, and include tedious validation code to produce the error. Andrew’s “hgsetgetplus” class makes this easy. When you write your own classes, you simply inherit from the “hgsetgetplus” class, and define the attributes of your class’s properties with this simple clean interface:

% Code that sets property constraints for a hypothetical % 'LineStyle' class. This code is inside of the CLASSDEF file.
obj.width.classes = {'numeric'}; obj.width.attributes = {'integer', '>', 0}; obj.style.classes = {'char'}; obj.style.choices = {'solid', 'dotted', 'dashed'};

This is what happens when we manipulate an object from the above class:

% Code that attempts to set the 'width' property of an % object from the above class.
>> set(obj, 'width', -5);
??? Error using ==> setOneProperty Expected width to be an array with all of the values > 0.

% Enumerates valid values for the 'style' property.
>> set(obj, 'style') [ solid | dotted | dashed ]

Andrew’s submission includes an example object and script that you can run to see behavior similar to the example above.

I like this submission for several reasons:

  1. It increases your productivity as a MATLAB programmer.
  2. It makes your code more reliable by reducing a common, and difficult to find, source of bugs.
  3. In addition to displaying a helpful error message, it also allows users to view the universe of valid property values, thus avoiding the error message in the first place, and improving their self esteem.
  4. Back when I helped staff our technical support line, somebody called and requested that we add this exact feature!
  5. It’s a great example of how to arrange and package a MATLAB utility. It includes clear documentation in HTML format, example code, and even unit tests that can be run with Steve Eddins’s xUnit framework. The content is grouped into separate folders, keeping things organized. If you find yourself developing tools in MATLAB for others to use, this is a good illustration of how you can bundle everything up cleanly.

Comment

Let us know what you think here or leave a comment for Andrew.

2 CommentsOldest to Newest

i wanted to know how to get values that are selected from pop-up menus,store them and display the corresponding image at the end(eg:if user selects various parts of the face than at the end the entire face should be dispalyed)

Hi Kamlesh,

I’m not quite sure what you mean by storing pop-up menus as an image, but you can get the value of a pop-up menu with code like this:

function popupmenu_Callback(h,e)
val             = get(h,'Value');
string_list     = get(h,'String');
selected_string = string_list{val}; % Convert from cell array
                                    % to string
% Proceed with callback...

If you want to display the selected menu item in a set of plotting axes, you can create a TEXT object in the axes.

This is also a good opportunity to plug MATLAB Answers and the MATLAB newsgroup (comp.soft-sys.matlab). You can often get quick answers to technical questions from these resources!

These postings are the author's and don't necessarily represent the opinions of MathWorks.