필터 지우기
필터 지우기

Invalid Handle Object with Subplot

조회 수: 4 (최근 30일)
Erin MacMillan
Erin MacMillan 2011년 10월 28일
Hi, I would appreciate any help on this matter, thank you for reading :)
I am attempting to do something like the following:
x1 = [1 2 3 4];
y1 = [2 4 6 8];
y2 = [4 8 12 16];
figure; hold on;
h1 = subplot(1,2,1);
hold on;
plot(x1,y1)
h2 = subplot(1,2,2);
hold on;
plot(x1,y2);
set(h1,'title','test1');
set(h2,'title','test2');
However, I get the error:
??? Error using ==> set Value must be a handle
I have also attempted to set both the figure and subplot properties to include 'NextPlot','add' but this did not help.
Thanks for all suggestions!

채택된 답변

Daniel Shub
Daniel Shub 2011년 10월 28일
The problem is that the property title requires a handle to a text object and not a string. The help http://www.mathworks.com/help/releases/R2011a/techdoc/ref/axes_props.html deals with this. You can create a text object in the appropriate axis with:
axes(h1);
set(h1,'title',text('string', 'test1'));
axes(h2);
set(h2,'title',text('string', 'test2'));
You might just want to use:
title(h1, 'test1');
  댓글 수: 3
Daniel Shub
Daniel Shub 2011년 10월 28일
That is because you left out the axes(h1) part. The text command puts the text into the current axes (in this case h2) and you want it in h1.
Erin MacMillan
Erin MacMillan 2011년 10월 28일
Yes that was it! Thank you very much!!

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

추가 답변 (1개)

the cyclist
the cyclist 2011년 10월 28일
Are you just trying to set titles of each subplot?
x1 = [1 2 3 4];
y1 = [2 4 6 8];
y2 = [4 8 12 16];
figure; hold on;
h1 = subplot(1,2,1);
title('test1')
hold on;
plot(x1,y1)
h2 = subplot(1,2,2);
title('test2')
hold on;
plot(x1,y2);
Part of the problem of the way you were trying to do it was that h1 and h2 are the handles for the lines plotted, not for the plotting axes.
  댓글 수: 2
Erin MacMillan
Erin MacMillan 2011년 10월 28일
Hi,
I am actually trying to do something a lot more complicated, but I boiled the problem down to that example. I need to set some properties of a subplot axes when the plots are run in a loop, so the handles get overwritten. I think you have stated the root of the problem: "h1 and h2 are the handles for the lines plotted, not for the plotting axes". How do I make a handle for the plotting axes in a subplot?
Thanks!
Daniel Shub
Daniel Shub 2011년 10월 28일
The handles h1 and h2 are handles to the axes since they are returned by subplot. The problem is that the title property does not want a string, but rather a handle.

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

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by