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');
Thanks in advance
No products are associated with this question.
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
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');
@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.
0 Comments