object properties treated as structures
1 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Guillaume
2015-4-14
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 个评论
Guillaume
2015-4-14
编辑:Guillaume
2015-4-14
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
更多回答(0 个)
另请参阅
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!