Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Text" function very slow, bottleneck of my code

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')
like image 385
Millemila Avatar asked Oct 04 '13 23:10

Millemila


1 Answers

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');

and my life changed.

Thanks. A.

like image 156
Millemila Avatar answered Oct 25 '22 15:10

Millemila