MATLAB function run in 2021a producing graphics handles as unique doubles (pre-2014 behavior) rather than handle objects

7 views (last 30 days)
I *just* discovered that MATLAB even had a different way of handling graphics in previous releases, as i began using MATLAB in 2020. I discovered it now because i am running a great user-contributed function, superbar by Scott Lowe, where various aspects of a custom barplot are output as separate handles for further control. My question is: despite this being an older function (released in 2016), why am i observing MATLAB 2021a (v i'm currently running) produce graphics handles as unique doubles rather than handle objects? And is there a way to make MATLAB 2021a treat assigned handles using the updated behavior i am used to post-2014? (i have literally never observed this unique double method)... i even tried to make a new version of this function to save locally, hoping that it would somehow be viewed as an 'updated' (?) function and ignore any versions possibly defaulted into the build.

Accepted Answer

Steven Lord
Steven Lord on 24 Apr 2022
Without seeing what you're doing I'm only guessing, but I suspect you're preallocating an array as a double array and assigning a graphics handle into it using subscripted assignment. When you do this, MATLAB tries to convert the thing you're assigning into the array into the type of the thing into which you're assigning.
So if I have a double value:
x = pi
x = 3.1416
that I'm trying to assign into an int32 array, that double will be converted into an int32 value.
y = zeros(1, 3, 'int32')
y = 1×3
0 0 0
class(y)
ans = 'int32'
y(2) = x % double pi gets converted into int32 3
y = 1×3
0 3 0
For backwards compatibility, MATLAB knows how to convert a graphics handle into a double value (which is how graphics handles were stored in the original Handle Graphics system.)
z = zeros(1, 3)
z = 1×3
0 0 0
class(z)
ans = 'double'
h = figure
h =
Figure (1) with properties: Number: 1 Name: '' Color: [1 1 1] Position: [671 661 577 433] Units: 'pixels' Show all properties
class(h)
ans = 'matlab.ui.Figure'
z(2) = h % the Figure object h is converted to double when stored in z
z = 1×3
0 1 0
get(z(2), 'Position') % Can use that double handle with get to retrieve properties
ans = 1×4
671 661 577 433
h.Position
ans = 1×4
671 661 577 433
If you're updating that code to use graphics objects you could preallocate the arrays into which you want to store graphics handles using gobjects instead of zeros.
g = gobjects(1, 3)
g =
1×3 GraphicsPlaceholder array: GraphicsPlaceholder GraphicsPlaceholder GraphicsPlaceholder
g(2) = h
g =
1×3 graphics array: GraphicsPlaceholder Figure GraphicsPlaceholder
  2 Comments
Connor Gallimore
Connor Gallimore on 24 Apr 2022
Outstanding. I am impressed with your intuition; you are exactly correct. Under the hood of the function i was using, graphics objects were being pre-allocated with
nan(size(data))
pre-allocating with:
gobjects(size(data))
produced exactly the result you suggest, and the one i was expecting. Accepted and upvoted, case closed! Hope folks who encounter this previous (and somewhat difficult to track) behavior find this page. Thanks Steven!

Sign in to comment.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!