MATLAB function run in 2021a producing graphics handles as unique doubles (pre-2014 behavior) rather than handle objects
6 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Steven Lord
2022-4-24
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
that I'm trying to assign into an int32 array, that double will be converted into an int32 value.
y = zeros(1, 3, 'int32')
class(y)
y(2) = x % double pi gets converted into int32 3
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)
class(z)
h = figure
class(h)
z(2) = h % the Figure object h is converted to double when stored in z
get(z(2), 'Position') % Can use that double handle with get to retrieve properties
h.Position
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(2) = h
2 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!