Clear Filters
Clear Filters

object properties treated as structures

2 views (last 30 days)
pfb
pfb on 14 Apr 2015
Commented: pfb on 14 Apr 2015
Hi all,
I always thought that you should use get and set to play with object properties.
From time to time I stumble on a strange syntax. I did not keep track of the places where I saw that, but it definitely happened to me quite a few times. One example in this respect can be found in a recent post here on matlab central.
What I'm talking about is something like
h.XData = [1 2 3];
as opposed to
set(h,'Xdata',[1 2 3]);
Is the first syntax a real thing? I tried that, but only got the expected "Attempt to reference field of non-structure array."
I'm asking because, maybe, I could find such a syntax useful in some cases.
For instance, sometimes I need to adjust the position of an axis. Perhaps reducing its width from the default 0.7750 to 0.75. What I do is
p=get(gca,'position');
p(3) = 0.75;
set(gca,'position',p);
Perhaps, if the above syntax was real, I could go
gca.position(3)=0.75;
Anyway, is there a way to change "in place" just one of the elements of a vector attribute of some object?

Accepted Answer

Guillaume
Guillaume on 14 Apr 2015
You must be using a version of matlab older than R2014b. In R2014b, the graphics have been overhauled and are now object oriented.
See this page detailing the changes. The first syntax is valid in HG2 and is actually nicer, since you get automatic tab completion of the property name (rather than having to remember a string).
  3 Comments
Guillaume
Guillaume on 14 Apr 2015
Edited: Guillaume on 14 Apr 2015
Yes, and no. You can't chain object properties (such as Position) to functions (such as gca) return values. That's a limitation of matlab. Also the properties are now case sensitive. So the following would work:
ax = gca; %returns the current Axes object
ax.Position(3) = 0.75; %change its width
pfb
pfb on 14 Apr 2015
Yes, I worried that this would not work with gca directly. But it's cool all the same.
Thanks a lot!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!