Can you set figure axis properties without using gca as argument to the set command?

10 views (last 30 days)
I know you can save a structure variable for the current axis using get(gca); however, that structure seems useless except to list items within it.
For example:
axishandle = get(gca); axishandle.XTick
will display the current x-axis tick values on the command line, but both of the following commands are invalid:
set(axishandle.XTick,[0 0.5 1]) set(axishandle.XTick,'XTick',[0 0.5 1])
However,
set(gca,[0 0.5 1])
is valid--so what is the point of being able to assign a handle to the current axis?

Accepted Answer

Robert Cumming
Robert Cumming on 9 Sep 2014
you are returning the axes properties as a static structure - which cant be edited in the way you have done it, however you could have done:
axishandle = gca
set ( axishandle, 'Xtick', [0 0.5 1] );
FYI: You could also have done:
axishandle = handle(gca); % returns as a handle object of the axes
axishandle.XTick = [0 0.5 1]
The second way is the way HG2 will work when it gets released (as far as I know).
As a note: When I create axes I save the handle to it - I hardly ever use gca for anything...
  1 Comment
Seth Wagenman
Seth Wagenman on 9 Sep 2014
I did not know gca could be used like that:
axishandle = gca
That is very useful and powerful. I also did not know about the handle command, which actually could save a few commas and apostrophes, etc. that are required to use the set command.

Sign in to comment.

More Answers (1)

Sean de Wolski
Sean de Wolski on 9 Sep 2014
I think you're misunderstanding the handle and output from get.
axHandle = gca
Will give you the handle to the axes. You can think of this as a phone number with which you can query or set the axes.
props = get(gca)
Gives you a static structure of what all of the properties are at the time. This does not update and you would have to rerun it any time you want current properties.
If you want a specific property (rather than all of them), just get it
xt = get(gca,'XTick')
The purpose of being able to store the handle to gca is so that you can have multiple axes and control which one gets the update or whose properties you're querying even if the axes you care about is not current.
axh1 = subplot(1,2,1);
axh2 = subplot(1,2,2);
Now regardless of which one is current I can grab pieces of either.
  1 Comment
Seth Wagenman
Seth Wagenman on 9 Sep 2014
Thanks for this answer! I did not realize you could assign handles via the subplot command as well...I thought that was only a way to tell MATLAB where to "draw" its next picture...

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!

Translated by