Mathworks Matlab R2009b
Solving a MATLAB bug by subclassing. I would like to welcome guest blogger Matthew Whitaker. Many of Matts CSSM submissions offer important insight of internal Matlab functionality. As shall be seen by todays article and some future submissions, Matt has plenty to share vis a vis Matlabs undocumented functionality. In my day to day work I make extensive use of MATLABs Image Processing Toolbox IPT. Mathworks Matlab R2009b FreeMatlabs Image Processing Toolboxs impoint function contains an annoying bug that can be fixed using some undocumented properties. Apache Iii Calculator. What is a cell array Edit. A cell is a flexible type of variable that can hold any type of variable. A cell array is simply an array of those cells. One area of the toolbox that has seen considerable change over the last few releases has been the development of a set of modular tools to aid in GUI design for image processing applications. In this article, I examine a bug in one of those tools to illustrate how we can use the power of subclassing these objects using an undocumented property to design a simple and effective workaround. The problem. The problem arose as I was refactoring some code that was written in R2. R2. 00. 9b. The code in question uses the impoint tool on an image along with an associated text object that moves with the point to display information as it is dragged around the image. At the time of the R2. Mathworks Matlab R2009b Crack' title='Mathworks Matlab R2009b Crack' />API. In R2. 00. 6b the call to impoint returns a handle to an hggroup containing a structure of function handles in its application data under the tag API. This programming pattern was common before the advent of the new class syntax in MATLAB version 7. R2. 00. 8a. Here is an example of how impoint would be used in R2. BehaviorR2. 00. 6b. IMPOINTBEHAVIORR2. B shows how impoint would be used in R2. Note RUN UNDER R2. B will run under R2. Display the image in a figure windowfigure imshowrice. In R2. 00. 6b calling impoint returns the hggroup handle. In 2. 00. 6b iptgetapi returns a structure of function handles. Add a new position callback to set the text string. Mathworks Matlab R2009b' title='Mathworks Matlab R2009b' />New. Position. Callbacknew. PosCallback. Construct boundary constraint function so we cant go outside the axes. Constrain. To. Rect. Bushido Und Die Mafia Stern Pdf. Fcnimpoint,getgca,XLim,getgca,YLim. Drag. Constraint. Fcnfcn. Fire callback so we get initial text. PosCallbackapi. Position. PosCallbacknew. Pos Display the current point position in a text label. Stringsprintf1. Pos1,new. Pos2. PosCallback. BehaviorR2. 00. 6b. The code above, when run in R2. In R2. 00. 9b, impoint is now a true MATLAB class using the new classdef 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 impoint. BehaviorR2. 00. 9b. IMPOINTBEHAVIORR2. B shows the undesirable behavior whenusing the set. String method in R2. Display the image in a figure windowfigure imshowrice. Add a new position callback to set the text string. New. Position. Callbacknew. PosCallback. Construct boundary constraint function so we cant go outside the axes. Constrain. To. Rect. Fcnimpoint,getgca,XLim,getgca,YLim. Enforce boundary constraint function. Position. Constraint. Fcnfcn. Fire callback so we get initial text. PosCallbackh. get. Position. function new. PosCallbacknew. Pos Display the current point position in a text label. Stringsprintf1. Pos1,new. Pos2endnew. PosCallback. BehaviorR2. 00. 9b. Unfortunately, when this code is run, dragging the mouse around the axes produces a trail of labels as shown below Before buggy behavior. Opening up impoint. String method creates a new text object each time it is used. I reported this to MATLAB and the bug is now documented on the Math. Works Support site 5. The solution. 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 set. String behavior we want. Looking at the impoint code we find that impoint is a subclass of imroi. In the imroi property declarations we see a number of undocumented properties that are protected. We can access these properties in a subclass but not outside the class. One of these undocumented properties is hgroup which is an hggroup that contains the handle graphic objects that make up the impoint on the screen. The label, when created, becomes part of this hggroup with its Tag property set to label. When performing the set. String method the behavior we want to see is that if the text object exists we want to update its String property. If it does not exist we want it to perform its existing functionality classdef impointtextupdate lt impoint. IMPOINTTEXTUPDATE subclasses impoint to override the set. Stringmethod of impoint so that it does not create a new text objecteach time it is called. Stringobj,stroverride impoint set. String. check to see if there is already a text label. Type,text,Tag,label. Stringimpointobj,str. String,str. endifendset. String. endmethodsendimpointtextupdate. Substituting calls to impoint with the new impointupdatetext subclass now produces the desired effect as shown below After expected behavior. Conclusions. 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 impoint while overriding the undesirable behavior. 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 Ive 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. Matlab R2. 00. 9b MATLAB.