File Exchange Pick of the Week

November 7th, 2005

Get In Units

How many times have you written code that looks like this, just to find the position of a graphics object in something other than its current units?

hAxes = axes;
currentUnits = get(hAxes,'Units');
set(hAxes,'Units','Pixels');
axesPositionPixels = get(hAxes,'Position');
set(hAxes,'Units',currentUnits)

My code is littered with fragments like this. While it works, there are two things I don’t like about my code:

  1. 3 out of 4 lines have nothing to do with getting the information I want
  2. It’s not the least bit obvious what my code is doing

If I were smart, I would have figured out long ago that what I really wanted was a function that would return the property’s value in whatever units I specified. Fortunately, Jiro Doke recognized this coding pattern and developed a very convenient solution. getInUnits does exactly what its name says - it allows you to GET the value of any property that depends on Units or Font Units in whatever units you specify.

I also want to thank Jiro for his responsiveness. I emailed him with a very small suggestion after I came across this submission. He got right back to me, and posted an updated file almost immediately. Great stuff, Jiro!

Comments are closed.


Bob, Brett & Jiro share their favorite user-contributed submissions from the File Exchange.

  • Zach: Hi Doug and Les, I didn’t have a lot of time to mess with this, but I did find a work-around. I plotted...
  • hamed: k
  • Les: @Zach This isn’t exactly what you are looking for but at least it puts all three parameters on the same...
  • Zach: Thanks for your suggestions Doug. I’ll give that a shot and see what happens. I’ve seen many of...
  • Doug: @Zach, I would say to use plotYYY, because that is close to what you want, but using depth as Y makes sense....
  • Doug: @Teja, I think this will work: http://www.mathworks .com/access/helpdesk /help/techdoc/ref...
  • Gify: merry christmas :) nice christmas tree! Regards, Janet Gify
  • Teja: Dear Doug Is there anyway to plot a surface from nonuniform data without meshgrid and griddata? Basically i...
  • Zach: I’m working with geophysical data, so I’d like to produce a depth profile. The y-axis would be...
  • Doug: @Ashok First, please do not use variable names that are MATLAB commands (std and mean). Second, p(j) should be...

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