**- Undocumented Matlab - https://undocumentedmatlab.com -**

Solving a MATLAB bug by subclassing

Posted By Yair Altman On February 7, 2010 | __4 Comments__

*I would like to welcome guest blogger Matthew Whitaker ^{[1]}. Many of Matt’s CSSM submissions offer important insight of internal Matlab functionality. As shall be seen by today’s article and some future submissions, Matt has plenty to share vis-a-vis Matlab’s undocumented functionality.*

In my day-to-day work I make extensive use of MATLAB’s Image Processing Toolbox

The problem arose as I was refactoring some code that was written in R2006b to R2009b. The code in question uses the ** impoint** tool on an image along with an associated

Here is an example of how

```
function impointBehavior_R2006b
%IMPOINTBEHAVIOR_R2006B shows how impoint would be used in R2006b
%Note: RUN UNDER R2006B (will run under R2009b but actually uses
%classdef impoint so it will show the same issue)
% Display the image in a figure window
figure; imshow('rice.png');
% In R2006b calling impoint returns the hggroup handle
h = impoint(gca,100,200);
% In 2006b iptgetapi returns a structure of function handles
api = iptgetapi(h);
% Add a new position callback to set the text string
api.addNewPositionCallback(@newPos_Callback);
% Construct boundary constraint function so we can't go outside the axes
fcn = makeConstrainToRectFcn('impoint',get(gca,'XLim'),get(gca,'YLim'));
api.setDragConstraintFcn(fcn);
% Fire callback so we get initial text
newPos_Callback(api.getPosition());
function newPos_Callback(newPos)
% Display the current point position in a text label
api.setString(sprintf('(%1.0f,%1.0f)',newPos(1),newPos(2)));
end %newPos_Callback
end %impointBehavior_R2006b
```

The code above, when run in R2006b, produces the desired behavior of displaying a text object containing the point coordinates that moves around with the point as it is dragged around the axes.

In R2009b, *impoint*^{[5]} is now a true MATLAB class using the new *classdef*^{[6]} syntax, so I wanted to update the existing code. Initially this appeared to be a straightforward translation of the code to make use of the new ** impoint** class syntax. The first attempt to rewrite the code was:

```
function impointBehavior_R2009b
%IMPOINTBEHAVIOR_R2009B shows the undesirable behavior when
%using the setString method in R2009b.
% Display the image in a figure window
figure; imshow('rice.png');
h = impoint(gca,100,200);
% Add a new position callback to set the text string
h.addNewPositionCallback(@newPos_Callback);
% Construct boundary constraint function so we can't go outside the axes
fcn = makeConstrainToRectFcn('impoint',get(gca,'XLim'),get(gca,'YLim'));
% Enforce boundary constraint function
h.setPositionConstraintFcn(fcn);
% Fire callback so we get initial text
newPos_Callback(h.getPosition());
function newPos_Callback(newPos)
% Display the current point position in a text label
h.setString(sprintf('(%1.0f,%1.0f)',newPos(1),newPos(2)))
end %newPos_Callback
end %impointBehavior_R2009b
```

Unfortunately, when this code is run, dragging the mouse around the axes produces a trail of labels as shown below:

Opening up

So how do we work around this bug to get to the behavior we want? One solution would be to rewrite the offending MATLAB code but this is somewhat risky in terms of maintainability and compatibility.

A more elegant solution is to subclass the ** impoint** class and substitute the

```
classdef impointtextupdate < impoint
%IMPOINTTEXTUPDATE subclasses impoint to override the setString
%method of impoint so that it does not create a new text object
%each time it is called.
methods
function obj = impointtextupdate(varargin)
obj = obj@impoint(varargin{:});
end %impointtextupdate
function setString(obj,str)
%override impoint setString
%check to see if there is already a text label
label = findobj(obj.h_group,'Type','text','Tag','label');
if isempty(label)
%do the default behavior
setString@impoint(obj,str);
else
%update the existing tag
set(label(1),'String',str);
end %if
end %setString
end %methods
end %impointtextupdate
```

Substituting calls to ** impoint** with the new

This case illustrates a couple of points:

- Much of the existing code in the MATLAB toolboxes is being updated to the new object oriented syntax. This presents many opportunities to easily and elegantly modify the default behavior without modifying provided toolbox code In the example above we retain all the desirable behavior of
while overriding the undesirable behavior.*impoint* - Many of the properties and methods in the provided toolbox objects are hidden or protected and are undocumented. It takes some simple detective work to find these out through examining the code. MATLAB is very generous in providing much of the existing code openly. Open the functions and classes you use in the editor to really find out how they work. Over the years I've learned and adopted a lot of useful MATLAB programming patterns by examining the code in the various toolboxes (there are a few coding horrors as well!).

I hope to explore some other under-documented features of the IPT and other toolboxes in future posts.

Categories: Guest bloggers, Hidden property, Low risk of breaking in future versions, Stock Matlab function, Toolbox

4 Comments (Open | Close)

Article printed from Undocumented Matlab: **https://undocumentedmatlab.com**

URL to article: **https://undocumentedmatlab.com/articles/solving-a-matlab-bug-by-subclassing**

URLs in this post:

[1] Matthew Whitaker: **https://www.mathworks.com/matlabcentral/newsreader/author/94021**

[2] Image Processing Toolbox: **http://www.mathworks.com/products/image/**

[3] modular tools: **http://www.mathworks.com/access/helpdesk/help/toolbox/images/f17-6011.html**

[4] ** hggroup**:

[5] ** impoint**:

[6] ** classdef**:

[7] 574846: **http://www.mathworks.com/support/bugreports/574846**

[8] ** imroi**:

[9] Solving a Matlab MCOS bug : **https://undocumentedmatlab.com/articles/solving-a-matlab-mcos-bug**

[10] Solving a Matlab hang problem : **https://undocumentedmatlab.com/articles/solving-a-matlab-hang-problem**

[11] Solving an mput (FTP) hang problem : **https://undocumentedmatlab.com/articles/solving-an-mput-ftp-hang-problem**

[12] Simple GUI Tabs for Advanced Matlab Trading App : **https://undocumentedmatlab.com/articles/simple-gui-tabs-for-advanced-matlab-trading-app**

[13] Speeding-up builtin Matlab functions – part 2 : **https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-2**

[14] Speeding-up builtin Matlab functions – part 1 : **https://undocumentedmatlab.com/articles/speeding-up-builtin-matlab-functions-part-1**

Click here to print.

Copyright © Yair Altman - Undocumented Matlab. All rights reserved.

4 Comments To "Solving a MATLAB bug by subclassing"

#1 CommentByFosterOn March 4, 2010 @ 07:13Good post, looking forward to more IPT-related posts in the future.

I’m new to this ‘detective work’ and have a quick q.. I’m using the impoly function, and trying to make the vertices smaller. I made the lines thinner by editing the (undocumented) polygonSymbol, but I can’t find the bit to change the points…keep getting dead ends. I’ve figured it uses impoint but I’m stuck on accessing the handle objects. Any *ahem* pointers?

#2 CommentByMatt WhitakerOn March 9, 2010 @ 12:54Hi Foster,

This is another good situation for subclassing.

Here is a pretty minimal class for setting the vertex size. Note it does not handle the situation of adding a new vertex or changing vertex color after creation. For now you can just call the setVertexSize method after doing one of those operations. That’s slightly more sophisticated and may be worth a complete blog entry.

Here we let it draw the polygon then find the vertices and change the marker size of the lines that make them up. Usage as follows:

A couple more points:

I really try to avoid editing any of the supplied code as it becomes a maintenance headache when you change versions.

Also a useful detective tool is the metaclass class

So mc = ?impoly gives you all sorts of useful info to help design you subclass.

Hope this Helps

Matt W

#3 CommentBysumaiaOn February 3, 2012 @ 04:27I am trying to run an example that uses validateattributes. My matlab is an older version and doesn’t recognise this function. Is it possible to load this function to my matlab and if so can you tell me how. I also need the function list.

Many thanks

#4 CommentByYair AltmanOn February 3, 2012 @ 04:51@sumaia –

simply ensures that the specified input variable is of the expected type. You can make these checks yourself, without really needing to usevalidateattributes. Or, if you are confident that the variables are of the expected type, you can skip these checks altogether.validateattributes