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

조회 수: 4 (최근 30일)
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?

채택된 답변

Robert Cumming
Robert Cumming 2014년 9월 9일
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
Seth Wagenman
Seth Wagenman 2014년 9월 9일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sean de Wolski
Sean de Wolski 2014년 9월 9일
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
Seth Wagenman
Seth Wagenman 2014년 9월 9일
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...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by