Programmatically calculate position for textbox (annotation)

I have been adding textbox annotations manually with an error and trial approach.
First I place the annotation and check wether it's in the correct position, then i modify the position manually and do it over again till I am satisfied.
Is it possible to calculate the positions in programmatical way?
I have the following figure:
The code for the rectangle grid is:
f = figure('color','w','un','pix','pos',[360 150 550 300]);
a = axes('un','pix','pos',[50,30,450,231],'fonts',8,'box','on',...
'Xlim',[0,54],'Xtick',[18 36],'XtickLabel',[],'XGrid','on',...
'GridL','-','Ylim',[0 5],'Ytick',1:1:4,'YtickLabel',[],...
'Ygrid','on');
  • I want to place the string test in the left lower corner of each rectangle, how can I do it?
  • How can I center over the column of rectangles the textboxes containing the date ranges?
Thanks in advance

 채택된 답변

Daniel Shub
Daniel Shub 2011년 7월 19일
It is a little bit of a kludge. I cannot decide if I want to include the x limits as xticks or not. Also, the char offset is really up to you. I think that 0.5 character units is about right.
f = figure('color','w','un','pix','pos',[360 150 550 300]);
a = axes('un','pix','pos',[50,30,450,231],'fonts',8,'box','on',...
'Xlim',[0,54],'Xtick',[18 36],'XtickLabel',[],'XGrid','on',...
'GridL','-','Ylim',[0 5],'Ytick',1:1:4,'YtickLabel',[],...
'Ygrid','on');
charoffset = 0.5;
xlabelarray = [min(xlim), get(a, 'XTick')]
for ixlabel = 1:length(xlabelarray);
h = text(xlabelarray(ixlabel), min(ylim), 'test');
set(h, 'Units', 'Characters');
set(h, 'Position', get(h, 'Position')+[charoffset, charoffset, 0])
end
charoffset = 0.5;
titlelabelarray = {'01 Jan 96 - 31 Jan 98'; ...
'01 Apr 98 - 31 Oct 09'; ...
'01 Jan 10 - 31 May 11'};
titlexarray = [min(xlim), get(a, 'XTick')]+diff([min(xlim), get(a, 'XTick'), max(xlim)])/2;
for ititle = 1:length(titlelabelarray);
h = text(titlexarray(ititle), max(ylim), titlelabelarray{ititle});
set(h, 'Units', 'Characters');
set(h, 'Position', get(h, 'Position')+[0, charoffset, 0]);
set(h, 'HorizontalAlignment', 'Center');
end

추가 답변 (1개)

Jan
Jan 2011년 7월 19일
Are the 'test' lables TEXT objects?
Do the rectangles always have the shown position or do you create them dynamically based on some values?
Perhaps this helps already:
TextH(1) = text(0.05, 0.05, 'test', ...
'Units', 'normalized');
TextH(2) = text(0.05 + 1/3, 0.05, 'test', ...
'Units', 'normalized');
TextH(3) = text(0.05 + 2/3, 0.05, 'test', ...
'Units', 'normalized');
TextH(4) = text(1/6, 1.05, 'Header 1', ...
'Units', 'normalized', ...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'bottom');
TextH(5) = text(1/3 + 1/6, 1.05, 'Header 2', ...
'Units', 'normalized', ...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'bottom');
TextH(6) = text(2/3 + 1/6, 1.05, 'Header 3', ...
'Units', 'normalized', ...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'bottom');

댓글 수: 6

This solution answers my question as well but Daniel was faster. Basically I had to use text instead of annotation because the reference position is with respect to the axes instead of the figure.
Thanks again +1
@Oleg: Please try what happens with 'character' units, if the figure is resized. E.g. PRINT might perform a silent resize.
@Jan: Nothing happens since Oleg set the units of the axis to be pixels. If the axis units are normalized, then you are correct, my text moves. You can fix this by setting the units of the text to be "data" or "normalized" after the position is set.
@Daniel: "data" units can fail, if the auto-resizing of the figure changes the axes limits. But "data" units are smart for zooming and panning. However, independent of the applied method to define the locations of text objects, there is always an export method, which distorts the layout :-(
@Oleg: I'm suffering from 1 sec keyboard latency and you pick the _faster_ answer. What a perplexing justice. :-)
@TMW: Please fix this latency stuff soon.
@Jan: as I commented on Daniel's answer I don't know much about character units and in general about graphing in matlab. I started to look into it recently and I already got nice results with print/export_fig to *.eps and I will continue and probably will post more questions.
I accepted his answer although yours is cleaner. I wanted to accept both or at least vote yours twice.
@Oleg: export_fig is a good idea!
As you I think that Daniel's answer is useful and usable. And it is worth to keep in mind that there are different methods for setting text locations. I was just kidding about the "justice". Actually my goal is a reduced latency.

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by