Changing subplot axis label using subplot handle

조회 수: 68 (최근 30일)
Earl
Earl 2012년 4월 16일
답변: Nguyen Thien An 2019년 12월 23일
I have a figure with 2 subplots, and I'd like to set the axis labels. I have assigned the handles P1 and P2 to the two subplots, e.g.
P1 = subplot(2,1,1);
P2 = subplot(2,1,2);
After plotting my data, I can do things like:
set(P1, 'YLim', [0 2])
but when I try changing the xlabel,
set(P1, 'Xlabel', 'This is the X label')
I get an error:
??? Error using ==> set Value must be a handle
I've tried changing case 'xlabel' and a few other things, but the documentation wasn't any help. Any ideas?

채택된 답변

Geoff
Geoff 2012년 4월 16일
That's because the value for 'XLabel' is actually a handle to another object. To see its properties, do this:
get( get(P1,'XLabel') );
Now, there's a property in there called 'String'. You should do this:
set( get(P1,'XLabel'), 'String', 'This is the X label' );
  댓글 수: 1
Earl
Earl 2012년 4월 16일
Thank you! I might have spent hours trying to work this out.

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

추가 답변 (2개)

Image Analyst
Image Analyst 2012년 4월 16일
Earl, there is a function called xlabel() and it takes a handle.
xlabel(P1, 'This is the X Label');
I believe this is the preferred, simpler way. If you set it right after you call subplot, then you don't even need to pass in the axes
suplot(1,2,1);
plot(x,y);
xlabel('X axis #1', 'FontSize', 20);
suplot(1,2,2);
plot(x,y);
xlabel('X axis #2', 'FontSize', 14);

Nguyen Thien An
Nguyen Thien An 2019년 12월 23일
This syntax work with me:
ax = subplot (2,2,1)
ax.XLabel.String = 'This is your label';

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by