Programmatically calculate position for textbox (annotation)

Asked by Oleg Komarov on 19 Jul 2011
Latest activity Commented on by Jan Simon on 20 Jul 2011

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

0 Comments

Oleg Komarov

Products

No products are associated with this question.

2 Answers

Answer by Daniel on 19 Jul 2011
Accepted answer

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 Comment

Oleg Komarov on 20 Jul 2011

Never used units character, nice suggestion.

Daniel
Answer by Jan Simon on 19 Jul 2011

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 Comments

Jan Simon on 20 Jul 2011

@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.

Oleg Komarov on 20 Jul 2011

@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.

Jan Simon on 20 Jul 2011

@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.

Jan Simon

Contact us