How do I add refreshable text label to my plot?

Hello,
After searching the web and the Q/A in your site I have decided to beat the experts. I am creating bar figure and I would like to see the value on top of each bar. My problem is that my figure refreshes every 2 seconds via timer (which is good), but the text on the bars is accumulating. I tried all values of 'erasemode' but it didn't help. I also tried to do h = text(...) and then delete h, but it didn't solve anything. Sorry to disturb if there is a similar question and solution.
Thanks in advance, Liad.

 채택된 답변

Laura Proctor
Laura Proctor 2011년 5월 11일

0 개 추천

I tried the following and it worked:
h = text(1,1,'hi there')
delete(h)
So I'm wondering if you would be able to show a simple example to help explain the issue.

추가 답변 (2개)

Fangjun Jiang
Fangjun Jiang 2011년 5월 11일

1 개 추천

set(h,'String','this is new text')
sco1
sco1 2011년 5월 11일

0 개 추천

The way your question reads, I'm assuming you are annotating the plot itself using the text() function?
Are you using hold?

댓글 수: 5

Liad
Liad 2011년 5월 11일
Thanks for the quick answers!
As I mentioned, I would like the text to get refreshed every cycle.
For now it works partially, and when I add 'h(patIx) = text', it doesn't work at all.
Any ideas?
Thank you.
Here is a part of my code:
% Declare the handles once
persistent hbar;
persistent h;
persistent ht;
%initialize
if isempty(hbar)
for patIx = 1:6
h(patIx) = subplot( 6, 1, patIx,'Parent',handleAxes );
hbar(patIx) = bar(lineNum, data(:,patIx), 0.01,'Parent',h(patIx));
end
end
try
for patIx = 1:6
set (hbar(patIx), 'XData', lineNum);
set (hbar(patIx), 'YData', data(:,patIx));
if ~isempty(ht)
delete (ht);
end
ht = text(lineNum, data(:,patIx),num2str(data(:,patIx)), 'Parent', hbar(patIx),'EraseMode','background');
end
catch
disp('Error in plot 6 subplots');
end
One thing that I see you are testing is if ht isempty. This probably isn't the way to test. Perhaps a test such as ishandle would be more appropriate. Because once you delete ht, it does still exist and will contain a number, it's just that it's no longer a handle object.
I don't understand. If you want to change the string of your text element and you have its handle, why can't you use
set(ht,'String',num2str(data(:,patlx)))
Liad
Liad 2011년 5월 12일
Hi,
I tried to move the next line to my initialize section, and do the 'set' in the 'for' loop:
ht(patIx) = text(lineNum, data(:,patIx),num2str(data(:,patIx)), 'Parent', hbar(patIx),'EraseMode','background');
But it keeps crashing even in the initialize part.
Does the 'set' allow to change the position of the text? Because I would like it in the height of my bar each time the plot gets refreshed.
Thank you,
Liad.
Once you created the text() element and get its handle ht(patlx), you can type get(ht(patlx)) in the Matlab command window to see all its properties. Then you can use set() to change most of the properties.
Start with the simplest option to create the text() element, step through to understand how it works and gradually add more options.
What do you mean "crashing"? Does it cause errors? or the figure does not show correctly? use the command drawnow() may help refreshing the figure.

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

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

질문:

2011년 5월 11일

Community Treasure Hunt

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

Start Hunting!

Translated by