问题描述
我正在处理结构化网格.我只想在图上添加类型(m,n)的文本,该文本指示每个节点的索引.也许将来会改为使用变量的值.我使用文字功能.我分析了代码,大部分时间都花在了该功能上.它只是一个101 * 101的网格,如果增加它,代码基本上会卡住.我已经对其进行了优化,避免了文本和spritnf的循环,但是它仍然太慢.而且,一旦创建了绘图,它就会非常卡住,并且每次平移或缩放都需要几秒钟的时间.请参阅以下的最小示例.我还添加了用于显示网格的补丁. (我之所以使用补丁,是因为我想为每个像元绘制一些网格数量,并且在要移动到具有不规则多边形的非结构化网格的情况下,我希望使其保持一般性.补丁是超快速的,没有任何概率).有什么建议可以加快速度吗?谢谢
I am dealing with a structured grid. I just wanna add to the plot a text of the type (m,n) that indicates the indices of each node. And maybe in the future the value of the variable instead. I use the text function. I profiled the code and most of the time is spent in that function. It is only a 101*101 grid, if you increase it the code is basically stuck. I already optimized it avoiding loops for text and spritnf, but it still too slow. Moreover, once the plot is created it is very stuck and it takes a few seconds every time to pan or zoom. See on the follow a minimal example. I also added the patch that I use to display the grid. (I use patch because I want to plot some grid quantities for each cell and I want to keep it general in case I move to an unstructured mesh with irregular polygons. Patch is superfast though, no prob with it). Any suggestion to speed this up? thanks
%define grid and grid numbering
DX = 10 ; %=DY
mmax = 101; %= number of nodes in x
nmax = mmax %= number of nodes in y
[ x y ] = meshgrid(0:DX:DX*(mmax-1),0:DX:DX*(mmax-1)); %grid
[ mMAT nMAT ] = meshgrid(1:mmax,1:nmax); %grid numbering
%
%display patch
%
cont = 0
for m=2:mmax
for n=2:nmax
cont=cont+1;
Xpatch(1:4,cont) = [ x(n-1,m-1) ; x(n-1,m) ; x(n,m) ; x(n,m-1) ] ;% ii+1 since it has the BC
Ypatch(1:4,cont) = [ y(n-1,m-1) ; y(n-1,m) ; y(n,m) ; y(n,m-1) ] ;
Zpatch(cont) = 1;
end
end
hpatch3 = patch(Xpatch(:,:),Ypatch(:,:),Zpatch(:)');
%
% display node indices
%
charINPUT = regexp(sprintf('(%d,%d)\n',mMAT(:),nMAT(:)),'(?<=\s*)(\S*)(?=\n)','match'); % use regexp to vectorize sprintf and so avoid slow loops with sprintf
text(x(:),y(:),charINPUT(:),'Clipping', 'on');
set(gcf,'position',[9 40 1350 650])
set(gcf,'PaperPositionMode','auto')
推荐答案
我找到了解决方案.如果将hittest
设置为'off'
,则速度提高了100倍!!!!我是这样做的:
Guys I found the solutions. 100 times faster if you just set hittest
to 'off'
!!!!! I did this:
text(x(:), y(:), charINPUT(:), 'Clipping', 'on','hittest', 'off');
,我的生活改变了.
谢谢.答:
这篇关于“文本"功能很慢,我的代码瓶颈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!