How to set the Attribute possition in a annotation of type rectangle
2 次查看(过去 30 天)
显示 更早的评论
I made a figure, in that I generated a grid of size say N=5 M=8, and I want to highlight the region between (5,2:end-1) in other color. What I did was make the grid in matlab and then generate the M-file for the case rows=N=5 columns=M=8, then in that figure I insert a rectangle with other color in the area I want to highlig and generate the M-file of the rectangle to obtain its position, and add it at the end of the grid code . Now what I want to do is generalize the code for any value of M or N, so for any value of N, M of the grid the region between (N,2:end-1)is highlated in other color. I could generalize the code of the grid by replacing 5 by N and 8 by M. but I am having problems with the position of the rectangle which are the last two lines of the code. I can not figure out how to set the vertical position of the retangle in terms of the size of the grid (N,M) here some code:
function CreateFigureGrid(N,M)
%CREATEFIGURE
% Auto-generated by MATLAB on 19-Nov-2010 16:49:38
% Create figure
%figure1 = figure('PaperSize',[20.98 29.68]);
figure1 = figure();
% Create axes
axes1 = axes('Parent',figure1,'YTick',0:N,'YGrid','on',...
'XTick',0:M,...
'XGrid','on',...
'XTickLabel',[],...
'YTickLabel',[],...
'Position',[0.1368 0.1153 0.775 0.815],...
'PlotBoxAspectRatio',[M N 1],...
'GridLineStyle','-',...
'DataAspectRatio',[1 1 1]);
% Uncomment the following line to preserve the X-limits of the axes
xlim(axes1,[0 M]);
% Uncomment the following line to preserve the Y-limits of the axes
ylim(axes1,[0 N]);
box(axes1,'on');
hold on
text(0.4,0.5,'S','FontName','Courier New');
text(M-.6,.5,'G');
annotation(figure1,'rectangle','FaceColor',[0.8471 0.1608 0],...
'Position',[.1368+.775/M 0.2024 (.775/M)*(M-2) 0.127]);
% Here are some other examples on how the position change with
%other values of N and M
%[0.2351 0.2024 0.5774 0.127] M=8 N=5
% [0.2143 0.2659 0.619 0.1032] M=10 N=5
% [0.1771 0.3948 0.6949 0.05159] M=20 N=5
.1368 is the x-value of the low-left corner of the axis .775 is the width of the box, I try to do the same with the y-values but itdoes not work, I am missing somenthing maybe the aspect ratio is involved in any way but I could not figure it out I will appreciate any help
0 个评论
采纳的回答
Ivan van der Kroon
2011-5-17
Your annotation (the highlighted rectangle) has little to do with your axes in which you specify the grid. You can see it yourself when you try to zoom in: the grid changes accordingly but the rectangle just sits there. If you want something in the figure use rectangle() (see help file).
For instance I would suggest to change it to
rectangle('Position',[1 0 M-2 1],'FaceColor',[0.8471 0.1608 0])
I kept your color but the position starts at (1,0) since your want it 1 position to the right of the left border but touching the bottom and Matlab treats the bottomleft as the origin. Next it should be M-2 long and 1 high. Be wary; if you change your grid, change this rectangle accordingly.
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!