MATLAB: Programmatically calculate position for textbox (annotation)

annotation;position;programmaticaltextbox;

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

Best 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