Axes dot notation not available when used as property in (handle) classes (Matlab R2014b+)

2 views (last 30 days)
Since Matlab R2014b, dot notation is available for axes (see Axes Properties).
I can easily initialize an array of 2 Axes:
a(2) = axes;
a(1) = axes;
And then, for example, modify the second Axes object:
a(2).XLim = [-2 2];
But the same functionality seems not to be available in classes:
classdef AxesTest < handle
properties
ax
end
methods
function this = AxesTest()
this.ax(2) = axes;
this.ax(1) = axes;
end
end
end
The ax property of the class is now a double array, as in earlier versions of Matlab:
a = AxesTest;
class(a.ax)
returns
ans =
double
Within classes, I have to use the old way of assigning new values to properties of Axes objects:
function makeYAxesEqual(this)
set(this.ax(1), 'YLim', get(this.ax(2), 'YLim'));
end
Is this an inconsistency of Matlab or is there a misunderstanding on my part?

Accepted Answer

Adam
Adam on 2 Dec 2015
Edited: Adam on 2 Dec 2015
That does surprise me. I use axes inside classes all over the place, but not all that often in arrays.
The following works:
classdef AxesTest < handle
properties
ax = matlab.graphics.axis.Axes.empty;
end
methods
function this = AxesTest()
this.ax(2) = axes;
this.ax(1) = axes;
end
end
end
and is, I suspect, why I haven't had a problem with this as my habit is to always initialise my properties with their type.
I couldn't work out exactly what was happening when I stepped through (even when I assigned the axes in 1, 2 order they ended up as doubles), but I assume it has something to do with arrays being of doubles by default and since axes handles can still be converted to doubles this is how they are being interpreted because this.ax was already created as an array of doubles by default before the assignments took place.
I may be wrong with that, it is partly supposition, but as mentioned, I prefer to be explicit with my property types when defining them and this seems to work.
If you prefer you can also put that initialisation line inside your constructor (with 'this' prepended obviously) and that will also work. The above is just my stylistic preference. You can also pre-size it if you wish as e.g.
ax = matlab.graphics.axis.Axes.empty(0,2)
  1 Comment
Lasse Osterhagen
Lasse Osterhagen on 2 Dec 2015
Thank you, Adam! This makes it clear to me. I suppose, you are correct in thinking that some implicit type conversion is happening, when assigning axes to the not explicitly initialized class property.

Sign in to comment.

More Answers (0)

Categories

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

Products

Community Treasure Hunt

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

Start Hunting!